PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/modules/societe/doc/doc_generic_odt.modules.php

https://github.com/asterix14/dolibarr
PHP | 330 lines | 208 code | 46 blank | 76 comment | 25 complexity | 68a9e7371f6889b74209e11b4a54035b MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2010-2011 Laurent Destailleur <ely@users.sourceforge.net>
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. * or see http://www.gnu.org/
  16. */
  17. /**
  18. * \file htdocs/core/modules/societe/doc/doc_generic_odt.modules.php
  19. * \ingroup societe
  20. * \brief File of class to build ODT documents for third parties
  21. * \author Laurent Destailleur
  22. */
  23. require_once(DOL_DOCUMENT_ROOT."/core/modules/societe/modules_societe.class.php");
  24. require_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php");
  25. require_once(DOL_DOCUMENT_ROOT."/core/lib/company.lib.php");
  26. require_once(DOL_DOCUMENT_ROOT."/core/lib/files.lib.php");
  27. /**
  28. * \class doc_generic_odt
  29. * \brief Class to build documents using ODF templates generator
  30. */
  31. class doc_generic_odt extends ModeleThirdPartyDoc
  32. {
  33. var $emetteur; // Objet societe qui emet
  34. var $phpmin = array(5,2,0); // Minimum version of PHP required by module
  35. /**
  36. * Constructor
  37. *
  38. * @param DoliDB $db Database handler
  39. */
  40. function doc_generic_odt($db)
  41. {
  42. global $conf,$langs,$mysoc;
  43. $langs->load("main");
  44. $langs->load("companies");
  45. $this->db = $db;
  46. $this->name = "ODT templates";
  47. $this->description = $langs->trans("DocumentModelOdt");
  48. $this->scandir = 'COMPANY_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
  49. // Dimension page pour format A4
  50. $this->type = 'odt';
  51. $this->page_largeur = 0;
  52. $this->page_hauteur = 0;
  53. $this->format = array($this->page_largeur,$this->page_hauteur);
  54. $this->marge_gauche=0;
  55. $this->marge_droite=0;
  56. $this->marge_haute=0;
  57. $this->marge_basse=0;
  58. $this->option_logo = 1; // Affiche logo
  59. // Recupere emmetteur
  60. $this->emetteur=$mysoc;
  61. if (! $this->emetteur->pays_code) $this->emetteur->pays_code=substr($langs->defaultlang,-2); // Par defaut, si n'etait pas defini
  62. }
  63. /**
  64. * Return description of a module
  65. *
  66. * @param Translate $langs Object language
  67. * @return string Description
  68. */
  69. function info($langs)
  70. {
  71. global $conf,$langs;
  72. $langs->load("companies");
  73. $langs->load("errors");
  74. $form = new Form($db);
  75. $texte = $this->description.".<br>\n";
  76. $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  77. $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  78. $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
  79. $texte.= '<input type="hidden" name="param1" value="COMPANY_ADDON_PDF_ODT_PATH">';
  80. $texte.= '<table class="nobordernopadding" width="100%">';
  81. // List of directories area
  82. $texte.= '<tr><td>';
  83. $texttitle=$langs->trans("ListOfDirectories");
  84. $listofdir=explode(',',preg_replace('/[\r\n]+/',',',trim($conf->global->COMPANY_ADDON_PDF_ODT_PATH)));
  85. $listoffiles=array();
  86. foreach($listofdir as $key=>$tmpdir)
  87. {
  88. $tmpdir=trim($tmpdir);
  89. $tmpdir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
  90. if (! $tmpdir) { unset($listofdir[$key]); continue; }
  91. if (! is_dir($tmpdir)) $texttitle.=img_warning($langs->trans("ErrorDirNotFound",$tmpdir),0);
  92. else
  93. {
  94. $tmpfiles=dol_dir_list($tmpdir,'files',0,'\.odt');
  95. if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles);
  96. }
  97. }
  98. $texthelp=$langs->trans("ListOfDirectoriesForModelGenODT");
  99. // Add list of substitution keys
  100. $texthelp.='<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
  101. $texthelp.=$langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
  102. $texte.= $form->textwithpicto($texttitle,$texthelp,1,'help','',1);
  103. $texte.= '<table><tr><td>';
  104. $texte.= '<textarea class="flat" cols="60" name="value1">';
  105. $texte.=$conf->global->COMPANY_ADDON_PDF_ODT_PATH;
  106. $texte.= '</textarea>';
  107. $texte.= '</td>';
  108. $texte.= '<td align="center">&nbsp; ';
  109. $texte.= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
  110. $texte.= '</td>';
  111. $texte.= '</tr>';
  112. $texte.= '</table>';
  113. // Scan directories
  114. if (count($listofdir)) $texte.=$langs->trans("NumberOfModelFilesFound").': <b>'.count($listoffiles).'</b>';
  115. $texte.= '</td>';
  116. $texte.= '<td valign="top" rowspan="2">';
  117. $texte.= $langs->trans("ExampleOfDirectoriesForModelGen");
  118. $texte.= '</td>';
  119. $texte.= '</tr>';
  120. /*$texte.= '<tr><td align="center">';
  121. $texte.= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
  122. $texte.= '</td>';
  123. $texte.= '</tr>';*/
  124. $texte.= '</table>';
  125. $texte.= '</form>';
  126. return $texte;
  127. }
  128. /**
  129. * Function to build a document on disk using the generic odt module.
  130. *
  131. * @param Societe $object Object source to build document
  132. * @param Translate $outputlangs Lang output object
  133. * @param string $srctemplatepath Full path of source filename for generator using a template file
  134. * @return int 1 if OK, <=0 if KO
  135. */
  136. function write_file($object,$outputlangs,$srctemplatepath)
  137. {
  138. global $user,$langs,$conf,$mysoc;
  139. if (empty($srctemplatepath))
  140. {
  141. dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
  142. return -1;
  143. }
  144. if (! is_object($outputlangs)) $outputlangs=$langs;
  145. $sav_charset_output=$outputlangs->charset_output;
  146. $outputlangs->charset_output='UTF-8';
  147. $outputlangs->load("main");
  148. $outputlangs->load("dict");
  149. $outputlangs->load("companies");
  150. $outputlangs->load("projects");
  151. if ($conf->societe->dir_output)
  152. {
  153. // If $object is id instead of object
  154. if (! is_object($object))
  155. {
  156. $id = $object;
  157. $object = new Societe($this->db);
  158. $object->fetch($id);
  159. if ($result < 0)
  160. {
  161. dol_print_error($db,$object->error);
  162. return -1;
  163. }
  164. }
  165. $dir = $conf->societe->dir_output;
  166. $objectref = dol_sanitizeFileName($object->id);
  167. if (! preg_match('/specimen/i',$objectref)) $dir.= "/" . $objectref;
  168. if (! file_exists($dir))
  169. {
  170. if (create_exdir($dir) < 0)
  171. {
  172. $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
  173. return -1;
  174. }
  175. }
  176. if (file_exists($dir))
  177. {
  178. //print "srctemplatepath=".$srctemplatepath; // Src filename
  179. $newfile=basename($srctemplatepath);
  180. $newfiletmp=preg_replace('/\.odt/i','',$newfile);
  181. $newfiletmp=preg_replace('/template_/i','',$newfiletmp);
  182. $newfiletmp=preg_replace('/modele_/i','',$newfiletmp);
  183. $file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';
  184. //print "newdir=".$dir;
  185. //print "newfile=".$newfile;
  186. //print "file=".$file;
  187. //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
  188. create_exdir($conf->societe->dir_temp);
  189. // Open and load template
  190. require_once(ODTPHP_PATH.'odf.php');
  191. $odfHandler = new odf($srctemplatepath, array
  192. (
  193. 'PATH_TO_TMP' => $conf->societe->dir_temp,
  194. 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
  195. 'DELIMITER_LEFT' => '{',
  196. 'DELIMITER_RIGHT' => '}')
  197. );
  198. //print $odfHandler->__toString()."\n";
  199. // Make substitutions into odt of user info
  200. $tmparray=$this->get_substitutionarray_user($user,$outputlangs);
  201. //var_dump($tmparray); exit;
  202. foreach($tmparray as $key=>$value)
  203. {
  204. try {
  205. if (preg_match('/logo$/',$key)) // Image
  206. {
  207. //var_dump($value);exit;
  208. if (file_exists($value)) $odfHandler->setImage($key, $value);
  209. else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
  210. }
  211. else // Text
  212. {
  213. //print $key.' '.$value;exit;
  214. $odfHandler->setVars($key, $value, true, 'UTF-8');
  215. }
  216. }
  217. catch(OdfException $e)
  218. {
  219. // setVars failed, probably because key not found
  220. }
  221. }
  222. // Make substitutions into odt of mysoc info
  223. $tmparray=$this->get_substitutionarray_mysoc($mysoc,$outputlangs);
  224. //var_dump($tmparray); exit;
  225. foreach($tmparray as $key=>$value)
  226. {
  227. try {
  228. if (preg_match('/logo$/',$key)) // Image
  229. {
  230. //var_dump($value);exit;
  231. if (file_exists($value)) $odfHandler->setImage($key, $value);
  232. else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
  233. }
  234. else // Text
  235. {
  236. $odfHandler->setVars($key, $value, true, 'UTF-8');
  237. }
  238. }
  239. catch(OdfException $e)
  240. {
  241. // setVars failed, probably because key not found
  242. }
  243. }
  244. // Make substitutions into odt of thirdparty + external modules
  245. $tmparray=$this->get_substitutionarray_thirdparty($object,$outputlangs);
  246. complete_substitutions_array($tmparray, $outputlangs, $object);
  247. //var_dump($object->id); exit;
  248. foreach($tmparray as $key=>$value)
  249. {
  250. try {
  251. if (preg_match('/logo$/',$key)) // Image
  252. {
  253. if (file_exists($value)) $odfHandler->setImage($key, $value);
  254. else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
  255. }
  256. else // Text
  257. {
  258. $odfHandler->setVars($key, $value, true, 'UTF-8');
  259. }
  260. }
  261. catch(OdfException $e)
  262. {
  263. // setVars failed, probably because key not found
  264. }
  265. }
  266. // Write new file
  267. //$result=$odfHandler->exportAsAttachedFile('toto');
  268. $odfHandler->saveToDisk($file);
  269. if (! empty($conf->global->MAIN_UMASK))
  270. @chmod($file, octdec($conf->global->MAIN_UMASK));
  271. $odfHandler=null; // Destroy object
  272. return 1; // Success
  273. }
  274. else
  275. {
  276. $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
  277. return -1;
  278. }
  279. }
  280. return -1;
  281. }
  282. }
  283. ?>