/**
  *	Function to build a document on disk using the generic odt module.
  *
  *	@param	Societe		$object				Object source to build document
  *	@param	Translate	$outputlangs		Lang output object
  * 	@param	string		$srctemplatepath	Full path of source filename for generator using a template file
  *	@return	int         					1 if OK, <=0 if KO
  */
 function write_file($object, $outputlangs, $srctemplatepath)
 {
     global $user, $langs, $conf, $mysoc;
     if (empty($srctemplatepath)) {
         dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
         return -1;
     }
     if (!is_object($outputlangs)) {
         $outputlangs = $langs;
     }
     $sav_charset_output = $outputlangs->charset_output;
     $outputlangs->charset_output = 'UTF-8';
     $outputlangs->load("main");
     $outputlangs->load("dict");
     $outputlangs->load("companies");
     $outputlangs->load("projects");
     if ($conf->contrat->dir_output) {
         $soc = new Societe($this->db);
         $soc->fetch($object->socid);
         $dir = $conf->contrat->dir_output;
         $objectref = dol_sanitizeFileName($object->ref);
         if (!preg_match('/specimen/i', $objectref)) {
             $dir .= "/" . $objectref;
         }
         if (!file_exists($dir)) {
             if (create_exdir($dir) < 0) {
                 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
                 return -1;
             }
         }
         if (file_exists($dir)) {
             //print "srctemplatepath=".$srctemplatepath;	// Src filename
             $newfile = basename($srctemplatepath);
             $newfiletmp = preg_replace('/\\.odt/i', '', $newfile);
             $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
             $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
             $file = $dir . '/' . $newfiletmp . '.' . dol_print_date(dol_now(), '%Y%m%d%H%M%S') . '.odt';
             //print "newdir=".$dir;
             //print "newfile=".$newfile;
             //print "file=".$file;
             //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
             create_exdir($conf->contrat->dir_temp);
             // If BILLING contact defined on invoice, we use it
             $usecontact = false;
             $arrayidcontact = $object->getIdContact('external', 'SALESREPSIGN');
             if (count($arrayidcontact) > 0) {
                 $usecontact = true;
                 $result = $soc->fetch_contact($arrayidcontact[0]);
             }
             // Recipient name
             if (!empty($usecontact)) {
                 // On peut utiliser le nom de la societe du contact
                 if ($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) {
                     $socobject = $object->contact;
                 } else {
                     $socobject = $soc->client;
                 }
             } else {
                 $socobject = $soc->client;
             }
             // Make substitution
             $substitutionarray = array('__FROM_NAME__' => $this->emetteur->nom, '__FROM_EMAIL__' => $this->emetteur->email, '__TOTAL_TTC__' => $object->total_ttc, '__TOTAL_HT__' => $object->total_ht, '__TOTAL_VAT__' => $object->total_vat, 'date' => dol_print_date($object->date_contrat, "%d %b %Y"));
             complete_substitutions_array($substitutionarray, $langs, $object);
             // Open and load template
             require_once ODTPHP_PATH . 'odf.php';
             $odfHandler = new odf($srctemplatepath, array('PATH_TO_TMP' => $conf->contrat->dir_temp, 'ZIP_PROXY' => 'PclZipProxy', 'DELIMITER_LEFT' => '{', 'DELIMITER_RIGHT' => '}'));
             //print $odfHandler->__toString()."\n";
             //
             foreach ($substitutionarray as $key => $value) {
                 try {
                     if (preg_match('/logo$/', $key)) {
                         //var_dump($value);exit;
                         if (file_exists($value)) {
                             $odfHandler->setImage($key, $value);
                         } else {
                             $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
                         }
                     } else {
                         //print $key.' '.$value;exit;
                         $odfHandler->setVars($key, $value, true, 'UTF-8');
                     }
                 } catch (OdfException $e) {
                 }
             }
             // Make substitutions into odt of user info
             $tmparray = $this->get_substitutionarray_user($user, $outputlangs);
             //var_dump($tmparray); exit;
             foreach ($tmparray as $key => $value) {
                 try {
                     if (preg_match('/logo$/', $key)) {
                         //var_dump($value);exit;
                         if (file_exists($value)) {
                             $odfHandler->setImage($key, $value);
                         } else {
                             $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
                         }
                     } else {
                         //print $key.' '.$value;exit;
                         $odfHandler->setVars($key, $value, true, 'UTF-8');
                     }
                 } catch (OdfException $e) {
                 }
             }
             // Make substitutions into odt of mysoc info
             $tmparray = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
             //var_dump($tmparray); exit;
             foreach ($tmparray as $key => $value) {
                 try {
                     if (preg_match('/logo$/', $key)) {
                         //var_dump($value);exit;
                         if (file_exists($value)) {
                             $odfHandler->setImage($key, $value);
                         } else {
                             $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
                         }
                     } else {
                         $odfHandler->setVars($key, $value, true, 'UTF-8');
                     }
                 } catch (OdfException $e) {
                 }
             }
             // Make substitutions into odt of thirdparty + external modules
             $tmparray = $this->get_substitutionarray_thirdparty($soc, $outputlangs);
             //complete_substitutions_array($tmparray, $langs, $object);
             //var_dump($object->id); exit;
             foreach ($tmparray as $key => $value) {
                 try {
                     if (preg_match('/logo$/', $key)) {
                         if (file_exists($value)) {
                             $odfHandler->setImage($key, $value);
                         } else {
                             $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
                         }
                     } else {
                         $odfHandler->setVars($key, $value, true, 'UTF-8');
                     }
                 } catch (OdfException $e) {
                 }
             }
             // Get extra fields for contractid
             include_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
             $hookmanager = new HookManager($this->db);
             $hookmanager->callHooks(array('contrat_extrafields'));
             $parameters = array('id' => $object->id);
             $values = $hookmanager->executeHooks('getFields', $parameters, $object, GETPOST('action'));
             // Note that $action and $object may have been modified by hook
             if (is_array($values)) {
                 foreach ($values as $key => $value) {
                     try {
                         if (preg_match("/^options_/", $key)) {
                             $var = substr($key, 8, strlen($key));
                             // retire options_
                             $odfHandler->setVars($var, $values[$key], true, 'UTF-8');
                         }
                     } catch (OdfException $e) {
                     }
                 }
             }
             // Get extra fields for socid
             include_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
             $hookmanager = new HookManager($this->db);
             $hookmanager->callHooks(array('thirdparty_extrafields'));
             $parameters = array('id' => $soc->id);
             $values = $hookmanager->executeHooks('getFields', $parameters, $soc, GETPOST('action'));
             // Note that $action and $object may have been modified by hook
             if (is_array($values)) {
                 foreach ($values as $key => $value) {
                     try {
                         if (preg_match("/^options_/", $key)) {
                             $var = substr($key, 8, strlen($key));
                             // retire options_
                             $odfHandler->setVars($var, $values[$key], true, 'UTF-8');
                         }
                     } catch (OdfException $e) {
                     }
                 }
             }
             // Write new file
             //$result=$odfHandler->exportAsAttachedFile('toto');
             $odfHandler->saveToDisk($file);
             if (!empty($conf->global->MAIN_UMASK)) {
                 @chmod($file, octdec($conf->global->MAIN_UMASK));
             }
             $odfHandler = null;
             // Destroy object
             return 1;
             // Success
         } else {
             $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
             return -1;
         }
     }
     return -1;
 }