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

/htdocs/core/modules/export/export_tsv.modules.php

https://github.com/asterix14/dolibarr
PHP | 246 lines | 124 code | 41 blank | 81 comment | 4 complexity | 18dac6b9233881d7be578e1761835876 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2006-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/modules/export/export_csv.modules.php
  19. * \ingroup export
  20. * \brief File of class to build export files with format TSV
  21. * \author Laurent Destailleur
  22. */
  23. require_once(DOL_DOCUMENT_ROOT ."/core/modules/export/modules_export.php");
  24. /**
  25. * \class ExportTsv
  26. * \brief Class to build export files with format TSV
  27. */
  28. class ExportTsv extends ModeleExports
  29. {
  30. var $id;
  31. var $label;
  32. var $extension;
  33. var $version;
  34. var $label_lib;
  35. var $version_lib;
  36. var $separator="\t";
  37. var $handle; // Handle fichier
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. */
  43. function ExportTsv($db)
  44. {
  45. global $conf;
  46. $this->db = $db;
  47. $this->id='tsv'; // Same value then xxx in file name export_xxx.modules.php
  48. $this->label='Tsv'; // Label of driver
  49. $this->desc='<b>Tab Separated Value</b> file format (.tsv)<br>This is a text file format where fields are separated by separator [tab].';
  50. $this->extension='tsv'; // Extension for generated file by this driver
  51. $this->picto='mime/other'; // Picto
  52. $this->version='1.15'; // Driver version
  53. // If driver use an external library, put its name here
  54. $this->label_lib='Dolibarr';
  55. $this->version_lib=DOL_VERSION;
  56. }
  57. function getDriverId()
  58. {
  59. return $this->id;
  60. }
  61. function getDriverLabel()
  62. {
  63. return $this->label;
  64. }
  65. function getDriverDesc()
  66. {
  67. return $this->desc;
  68. }
  69. function getDriverExtension()
  70. {
  71. return $this->extension;
  72. }
  73. function getDriverVersion()
  74. {
  75. return $this->version;
  76. }
  77. function getLibLabel()
  78. {
  79. return $this->label_lib;
  80. }
  81. function getLibVersion()
  82. {
  83. return $this->version_lib;
  84. }
  85. /**
  86. * Open output file
  87. *
  88. * @param string $file Path of filename to generate
  89. * @param Translate $outputlangs Output language object
  90. * @return int <0 if KO, >=0 if OK
  91. */
  92. function open_file($file,$outputlangs)
  93. {
  94. global $langs;
  95. dol_syslog("ExportTsv::open_file file=".$file);
  96. $ret=1;
  97. $outputlangs->load("exports");
  98. $this->handle = fopen($file, "wt");
  99. if (! $this->handle)
  100. {
  101. $langs->load("errors");
  102. $this->error=$langs->trans("ErrorFailToCreateFile",$file);
  103. $ret=-1;
  104. }
  105. return $ret;
  106. }
  107. /**
  108. * Output header into file
  109. *
  110. * @param Translate $outputlangs Output language object
  111. * @return int <0 if KO, >0 if OK
  112. */
  113. function write_header($outputlangs)
  114. {
  115. return 0;
  116. }
  117. /**
  118. * Output title line into file
  119. *
  120. * @param array $array_export_fields_label Array with list of label of fields
  121. * @param array $array_selected_sorted Array with list of field to export
  122. * @param Translate $outputlangs Object lang to translate values
  123. * @return int <0 if KO, >0 if OK
  124. */
  125. function write_title($array_export_fields_label,$array_selected_sorted,$outputlangs)
  126. {
  127. foreach($array_selected_sorted as $code => $value)
  128. {
  129. $newvalue=$outputlangs->transnoentities($array_export_fields_label[$code]);
  130. $newvalue=$this->tsv_clean($newvalue);
  131. fwrite($this->handle,$newvalue.$this->separator);
  132. }
  133. fwrite($this->handle,"\n");
  134. return 0;
  135. }
  136. /**
  137. * Output record line into file
  138. *
  139. * @param array $array_selected_sorted Array with list of field to export
  140. * @param resource $objp A record from a fetch with all fields from select
  141. * @param Translate $outputlangs Object lang to translate values
  142. * @return int <0 if KO, >0 if OK
  143. */
  144. function write_record($array_selected_sorted,$objp,$outputlangs)
  145. {
  146. $this->col=0;
  147. foreach($array_selected_sorted as $code => $value)
  148. {
  149. $alias=str_replace(array('.','-'),'_',$code);
  150. if (empty($alias)) dol_print_error('','Bad value for field with code='.$code.'. Try to redefine export.');
  151. $newvalue=$objp->$alias;
  152. // Translation newvalue
  153. if (preg_match('/^\((.*)\)$/i',$newvalue,$reg))
  154. {
  155. $newvalue=$outputlangs->transnoentities($reg[1]);
  156. }
  157. $newvalue=$this->tsv_clean($newvalue);
  158. fwrite($this->handle,$newvalue.$this->separator);
  159. $this->col++;
  160. }
  161. fwrite($this->handle,"\n");
  162. return 0;
  163. }
  164. /**
  165. * Output footer into file
  166. *
  167. * @param Translate $outputlangs Output language object
  168. * @return int <0 if KO, >0 if OK
  169. */
  170. function write_footer($outputlangs)
  171. {
  172. return 0;
  173. }
  174. /**
  175. * Close file handle
  176. *
  177. * @return int <0 if KO, >0 if OK
  178. */
  179. function close_file()
  180. {
  181. fclose($this->handle);
  182. return 0;
  183. }
  184. /**
  185. * Clean a cell to respect rules of TSV file cells
  186. *
  187. * @param string $newvalue String to clean
  188. * @return string Value cleaned
  189. */
  190. function tsv_clean($newvalue)
  191. {
  192. // Rule Dolibarr: No HTML
  193. $newvalue=dol_string_nohtmltag($newvalue);
  194. // Rule 1 TSV: No CR, LF in cells
  195. $newvalue=str_replace("\r",'',$newvalue);
  196. $newvalue=str_replace("\n",'\n',$newvalue);
  197. // Rule 2 TSV: If value contains tab, we must replace by space
  198. if (preg_match('/'.$this->separator.'/',$newvalue))
  199. {
  200. $newvalue=str_replace("\t"," ",$newvalue);
  201. }
  202. return $newvalue;
  203. }
  204. }
  205. ?>