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

/htdocs/core/modules/export/export_csv.modules.php

https://github.com/asterix14/dolibarr
PHP | 285 lines | 152 code | 51 blank | 82 comment | 8 complexity | 95978722f3116637b0ecf3851917ea71 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2006-2009 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 exports with CSV format
  21. * \author Laurent Destailleur
  22. */
  23. require_once(DOL_DOCUMENT_ROOT ."/core/modules/export/modules_export.php");
  24. /**
  25. * \class ExportCsv
  26. * \brief Class to build export files with format CSV
  27. */
  28. class ExportCsv 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;
  37. var $handle; // Handle fichier
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. */
  43. function ExportCsv($db)
  44. {
  45. global $conf,$langs;
  46. $this->db = $db;
  47. $this->separator=',';
  48. if (! empty($conf->global->EXPORT_CSV_SEPARATOR_TO_USE)) $this->separator=$conf->global->EXPORT_CSV_SEPARATOR_TO_USE;
  49. $this->escape='"';
  50. $this->enclosure='"';
  51. $this->id='csv'; // Same value then xxx in file name export_xxx.modules.php
  52. $this->label='Csv'; // Label of driver
  53. $this->desc=$langs->trans("CSVFormatDesc",$this->separator,$this->enclosure,$this->escape);
  54. $this->extension='csv'; // Extension for generated file by this driver
  55. $this->picto='mime/other'; // Picto
  56. $this->version='1.32'; // Driver version
  57. // If driver use an external library, put its name here
  58. $this->label_lib='Dolibarr';
  59. $this->version_lib=DOL_VERSION;
  60. }
  61. function getDriverId()
  62. {
  63. return $this->id;
  64. }
  65. function getDriverLabel()
  66. {
  67. return $this->label;
  68. }
  69. function getDriverDesc()
  70. {
  71. return $this->desc;
  72. }
  73. function getDriverExtension()
  74. {
  75. return $this->extension;
  76. }
  77. function getDriverVersion()
  78. {
  79. return $this->version;
  80. }
  81. function getLibLabel()
  82. {
  83. return $this->label_lib;
  84. }
  85. function getLibVersion()
  86. {
  87. return $this->version_lib;
  88. }
  89. /**
  90. * Open output file
  91. *
  92. * @param string $file Path of filename to generate
  93. * @param Translate $outputlangs Output language object
  94. * @return int <0 if KO, >=0 if OK
  95. */
  96. function open_file($file,$outputlangs)
  97. {
  98. global $langs;
  99. dol_syslog("ExportCsv::open_file file=".$file);
  100. $ret=1;
  101. $outputlangs->load("exports");
  102. $this->handle = fopen($file, "wt");
  103. if (! $this->handle)
  104. {
  105. $langs->load("errors");
  106. $this->error=$langs->trans("ErrorFailToCreateFile",$file);
  107. $ret=-1;
  108. }
  109. return $ret;
  110. }
  111. /**
  112. * Output header into file
  113. *
  114. * @param Translate $outputlangs Output language object
  115. * @return int <0 if KO, >0 if OK
  116. */
  117. function write_header($outputlangs)
  118. {
  119. return 0;
  120. }
  121. /**
  122. * Output title line into file
  123. *
  124. * @param array $array_export_fields_label Array with list of label of fields
  125. * @param array $array_selected_sorted Array with list of field to export
  126. * @param Translate $outputlangs Object lang to translate values
  127. * @return int <0 if KO, >0 if OK
  128. */
  129. function write_title($array_export_fields_label,$array_selected_sorted,$outputlangs)
  130. {
  131. global $conf;
  132. if (! empty($conf->global->EXPORT_CSV_FORCE_CHARSET))
  133. {
  134. $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
  135. }
  136. else
  137. {
  138. $outputlangs->charset_output = 'ISO-8859-1';
  139. }
  140. foreach($array_selected_sorted as $code => $value)
  141. {
  142. $newvalue=$outputlangs->transnoentities($array_export_fields_label[$code]);
  143. $newvalue=$this->csv_clean($newvalue);
  144. fwrite($this->handle,$newvalue.$this->separator);
  145. }
  146. fwrite($this->handle,"\n");
  147. return 0;
  148. }
  149. /**
  150. * Output record line into file
  151. *
  152. * @param array $array_selected_sorted Array with list of field to export
  153. * @param resource $objp A record from a fetch with all fields from select
  154. * @param Translate $outputlangs Object lang to translate values
  155. * @return int <0 if KO, >0 if OK
  156. */
  157. function write_record($array_selected_sorted,$objp,$outputlangs)
  158. {
  159. global $conf;
  160. if (! empty($conf->global->EXPORT_CSV_FORCE_CHARSET))
  161. {
  162. $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
  163. }
  164. else
  165. {
  166. $outputlangs->charset_output = 'ISO-8859-1';
  167. }
  168. $this->col=0;
  169. foreach($array_selected_sorted as $code => $value)
  170. {
  171. $alias=str_replace(array('.','-'),'_',$code);
  172. if (empty($alias)) dol_print_error('','Bad value for field with key='.$code.'. Try to redefine export.');
  173. $newvalue=$outputlangs->convToOutputCharset($objp->$alias);
  174. // Translation newvalue
  175. if (preg_match('/^\((.*)\)$/i',$newvalue,$reg))
  176. {
  177. $newvalue=$outputlangs->transnoentities($reg[1]);
  178. }
  179. $newvalue=$this->csv_clean($newvalue);
  180. fwrite($this->handle,$newvalue.$this->separator);
  181. $this->col++;
  182. }
  183. fwrite($this->handle,"\n");
  184. return 0;
  185. }
  186. /**
  187. * Output footer into file
  188. *
  189. * @param Translate $outputlangs Output language object
  190. * @return int <0 if KO, >0 if OK
  191. */
  192. function write_footer($outputlangs)
  193. {
  194. return 0;
  195. }
  196. /**
  197. * Close file handle
  198. *
  199. * @return int <0 if KO, >0 if OK
  200. */
  201. function close_file()
  202. {
  203. fclose($this->handle);
  204. return 0;
  205. }
  206. /**
  207. * Clean a cell to respect rules of CSV file cells
  208. *
  209. * @param string $newvalue String to clean
  210. * @return string Value cleaned
  211. */
  212. function csv_clean($newvalue)
  213. {
  214. $addquote=0;
  215. // Rule Dolibarr: No HTML
  216. $newvalue=dol_string_nohtmltag($newvalue);
  217. // Rule 1 CSV: No CR, LF in cells
  218. $newvalue=str_replace("\r",'',$newvalue);
  219. $newvalue=str_replace("\n",'\n',$newvalue);
  220. // Rule 2 CSV: If value contains ", we must escape with ", and add "
  221. if (preg_match('/"/',$newvalue))
  222. {
  223. $addquote=1;
  224. $newvalue=str_replace('"','""',$newvalue);
  225. }
  226. // Rule 3 CSV: If value contains separator, we must add "
  227. if (preg_match('/'.$this->separator.'/',$newvalue))
  228. {
  229. $addquote=1;
  230. }
  231. return ($addquote?'"':'').$newvalue.($addquote?'"':'');
  232. }
  233. }
  234. ?>