PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/import/core/modules/import/import_csv.modules.php

https://bitbucket.org/speedealing/speedealing
PHP | 291 lines | 133 code | 35 blank | 123 comment | 15 complexity | deb3a7fc2f21cbe740dfd12a0a1055a0 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * or see http://www.gnu.org/
  19. */
  20. require_once DOL_DOCUMENT_ROOT . '/import/core/modules/import/modules_import.php';
  21. /**
  22. * Class to import CSV files
  23. */
  24. class ImportCsv extends ModeleImports {
  25. var $db;
  26. var $datatoimport;
  27. var $error = '';
  28. var $errors = array();
  29. var $id; // Id of driver
  30. var $label; // Label of driver
  31. var $extension; // Extension of files imported by driver
  32. var $version; // Version of driver
  33. var $label_lib; // Label of external lib used by driver
  34. var $version_lib; // Version of external lib used by driver
  35. var $separator;
  36. var $handle; // Handle fichier
  37. var $cacheconvert = array(); // Array to cache list of value found after a convertion
  38. var $cachefieldtable = array(); // Array to cache list of value found into fields@tables
  39. /**
  40. * Constructor
  41. *
  42. * @param DoliDB $db Database handler
  43. * @param string $datatoimport String code describing import set (ex: 'societe_1')
  44. */
  45. function __construct($db, $datatoimport) {
  46. global $conf, $langs;
  47. $this->db = $db;
  48. $this->separator = ','; // Change also function cleansep
  49. if (!empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE))
  50. $this->separator = $conf->global->IMPORT_CSV_SEPARATOR_TO_USE;
  51. $this->enclosure = '"';
  52. $this->escape = '"';
  53. $this->id = 'csv'; // Same value then xxx in file name export_xxx.modules.php
  54. $this->label = 'Csv'; // Label of driver
  55. $this->desc = $langs->trans("CSVFormatDesc", $this->separator, $this->enclosure, $this->escape);
  56. $this->extension = 'csv'; // Extension for generated file by this driver
  57. $this->picto = 'mime/other'; // Picto
  58. $this->version = '1.34'; // Driver version
  59. // If driver use an external library, put its name here
  60. $this->label_lib = 'Dolibarr';
  61. $this->version_lib = DOL_VERSION;
  62. $this->datatoimport = $datatoimport;
  63. if (preg_match('/^societe_/', $datatoimport))
  64. $this->thirpartyobject = new Societe($this->db);
  65. }
  66. /**
  67. * getDriverId
  68. *
  69. * @return int Id
  70. */
  71. function getDriverId() {
  72. return $this->id;
  73. }
  74. /**
  75. * getDriverLabel
  76. *
  77. * @param string $key Key
  78. * @return string Label
  79. */
  80. function getDriverLabel($key = '') {
  81. return $this->label;
  82. }
  83. /**
  84. * getDriverDesc
  85. *
  86. * @param string $key Key
  87. * @return string Description
  88. */
  89. function getDriverDesc($key = '') {
  90. return $this->desc;
  91. }
  92. /**
  93. * getDriverExtension
  94. *
  95. * @return string Driver suffix
  96. */
  97. function getDriverExtension() {
  98. return $this->extension;
  99. }
  100. /**
  101. * getDriverVersion
  102. *
  103. * @param string $key Key
  104. * @return string Driver version
  105. */
  106. function getDriverVersion($key = '') {
  107. return $this->version;
  108. }
  109. /**
  110. * getDriverLabel
  111. *
  112. * @param string $key Key
  113. * @return string Label of external lib
  114. */
  115. function getLibLabel($key = '') {
  116. return $this->label_lib;
  117. }
  118. /**
  119. * getLibVersion
  120. *
  121. * @param string $key Key
  122. * @return string Version of external lib
  123. */
  124. function getLibVersion($key = '') {
  125. return $this->version_lib;
  126. }
  127. /**
  128. * Output header of an example file for this format
  129. *
  130. * @param Translate $outputlangs Output language
  131. * @return string
  132. */
  133. function write_header_example($outputlangs) {
  134. return '';
  135. }
  136. /**
  137. * Output title line of an example file for this format
  138. *
  139. * @param Translate $outputlangs Output language
  140. * @param array $headerlinefields Array of fields name
  141. * @return string
  142. */
  143. function write_title_example($outputlangs, $headerlinefields) {
  144. $s.=join($this->separator, array_map('cleansep', $headerlinefields));
  145. return $s . "\n";
  146. }
  147. /**
  148. * Output record of an example file for this format
  149. *
  150. * @param Translate $outputlangs Output language
  151. * @param array $contentlinevalues Array of lines
  152. * @return string
  153. */
  154. function write_record_example($outputlangs, $contentlinevalues) {
  155. $s = join($this->separator, array_map('cleansep', $contentlinevalues));
  156. return $s . "\n";
  157. }
  158. /**
  159. * Output footer of an example file for this format
  160. *
  161. * @param Translate $outputlangs Output language
  162. * @return string
  163. */
  164. function write_footer_example($outputlangs) {
  165. return '';
  166. }
  167. /**
  168. * Open input file
  169. *
  170. * @param string $file Path of filename
  171. * @return int <0 if KO, >=0 if OK
  172. */
  173. function import_open_file($file) {
  174. global $langs;
  175. $ret = 1;
  176. dol_syslog(get_class($this) . "::open_file file=" . $file);
  177. ini_set('auto_detect_line_endings', 1); // For MAC compatibility
  178. $this->handle = fopen(dol_osencode($file), "r");
  179. if (!$this->handle) {
  180. $langs->load("errors");
  181. $this->error = $langs->trans("ErrorFailToOpenFile", $file);
  182. $ret = -1;
  183. } else {
  184. $this->file = $file;
  185. }
  186. return $ret;
  187. }
  188. /**
  189. * Input header line from file
  190. *
  191. * @return int <0 if KO, >=0 if OK
  192. */
  193. function import_read_header() {
  194. return 0;
  195. }
  196. /**
  197. * Return array of next record in input file.
  198. *
  199. * @return Array Array of field values. Data are UTF8 encoded. [fieldpos] => (['val']=>val, ['type']=>-1=null,0=blank,1=string)
  200. */
  201. function import_read_record() {
  202. global $conf;
  203. $arrayres = array();
  204. if (version_compare(phpversion(), '5.3') < 0) {
  205. $arrayres = fgetcsv($this->handle, 100000, $this->separator, $this->enclosure);
  206. } else {
  207. $arrayres = fgetcsv($this->handle, 100000, $this->separator, $this->enclosure, $this->escape);
  208. }
  209. //var_dump($this->handle);
  210. //var_dump($arrayres);exit;
  211. $newarrayres = array();
  212. if ($arrayres && is_array($arrayres)) {
  213. foreach ($arrayres as $key => $val) {
  214. if (!empty($conf->global->IMPORT_CSV_FORCE_CHARSET)) { // Forced charset
  215. if (strtolower($conf->global->IMPORT_CSV_FORCE_CHARSET) == 'utf8') {
  216. $newarrayres[$key]['val'] = $val;
  217. $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
  218. } else {
  219. $newarrayres[$key]['val'] = utf8_encode($val);
  220. $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
  221. }
  222. } else { // Autodetect format (UTF8 or ISO)
  223. if (utf8_check($val)) {
  224. $newarrayres[$key]['val'] = $val;
  225. $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
  226. } else {
  227. $newarrayres[$key]['val'] = utf8_encode($val);
  228. $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
  229. }
  230. }
  231. }
  232. $this->col = count($newarrayres);
  233. }
  234. return $newarrayres;
  235. }
  236. /**
  237. * Close file handle
  238. *
  239. * @return void
  240. */
  241. function import_close_file() {
  242. fclose($this->handle);
  243. return 0;
  244. }
  245. }
  246. /**
  247. * Clean a string from separator
  248. *
  249. * @param string $value Remove separator
  250. * @return string String without separator
  251. */
  252. function cleansep($value) {
  253. return str_replace(',', '/', $value);
  254. }
  255. ;
  256. ?>