import.lib.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides some functions which can be used when importing data from
  5. * external files into Chamilo.
  6. * @package chamilo.library
  7. */
  8. /**
  9. * Class
  10. * @package chamilo.library
  11. */
  12. class Import {
  13. /**
  14. *
  15. * @param string $path
  16. * @return \CsvReader
  17. */
  18. static function csv_reader($path)
  19. {
  20. return new CsvReader($path);
  21. }
  22. /**
  23. * Reads a CSV-file into an array. The first line of the CSV-file should contain the array-keys.
  24. * The encoding of the input file is tried to be detected.
  25. * The elements of the returned array are encoded in the system encoding.
  26. * Example:
  27. * FirstName;LastName;Email
  28. * John;Doe;john.doe@mail.com
  29. * Adam;Adams;adam@mail.com
  30. * returns
  31. * $result [0]['FirstName'] = 'John';
  32. * $result [0]['LastName'] = 'Doe';
  33. * $result [0]['Email'] = 'john.doe@mail. com';
  34. * $result [1]['FirstName'] = 'Adam';
  35. * ...
  36. * @param string $filename The path to the CSV-file which should be imported.
  37. * @return array Returns an array (in the system encoding) that contains all data from the CSV-file.
  38. *
  39. *
  40. * @deprecated use csv_reader instead
  41. */
  42. static function csv_to_array($filename, $csv_order = 'vertical') {
  43. $result = array();
  44. // Encoding detection.
  45. $handle = fopen($filename, 'r');
  46. if ($handle === false) {
  47. return $result;
  48. }
  49. $buffer = array();
  50. $i = 0;
  51. while (!feof($handle) && $i < 200) {
  52. // We assume that 200 lines are enough for encoding detection.
  53. $buffer[] = fgets($handle);
  54. $i++;
  55. }
  56. fclose($handle);
  57. $buffer = implode("\n", $buffer);
  58. $from_encoding = api_detect_encoding($buffer);
  59. unset($buffer);
  60. // Reading the file, parsing and importing csv data.
  61. $handle = fopen($filename, 'r');
  62. if ($handle === false) {
  63. return $result;
  64. }
  65. if ($csv_order == 'vertical') {
  66. $keys = api_fgetcsv($handle, null, ';');
  67. foreach ($keys as $key => &$key_value) {
  68. $key_value = api_to_system_encoding($key_value, $from_encoding);
  69. }
  70. }
  71. while (($row_tmp = api_fgetcsv($handle, null, ';')) !== false) {
  72. $row = array();
  73. // Avoid empty lines in csv
  74. if (is_array($row_tmp) && count($row_tmp) > 0 && $row_tmp[0] != '') {
  75. if (!is_null($row_tmp[0])) {
  76. if ($csv_order == 'vertical') {
  77. foreach ($row_tmp as $index => $value) {
  78. $row[$keys[$index]] = api_to_system_encoding($value, $from_encoding);
  79. }
  80. } else {
  81. $first = null;
  82. $count = 1;
  83. foreach ($row_tmp as $index => $value) {
  84. if ($count == 1) {
  85. $first = $value;
  86. } else {
  87. $row[$first][] = api_to_system_encoding($value, $from_encoding);
  88. }
  89. $count++;
  90. }
  91. }
  92. $result[] = $row;
  93. }
  94. }
  95. }
  96. fclose($handle);
  97. return $result;
  98. }
  99. }