PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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