PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/model/resources/strategies/CSV.class.php

https://github.com/lievenjanssen/The-DataTank
PHP | 131 lines | 113 code | 7 blank | 11 comment | 3 complexity | 632890203b84c9b2bdd98dd74817f8a0 MD5 | raw file
  1. <?php
  2. /**
  3. * This class handles a CSV file
  4. *
  5. * @package The-Datatank/model/resources/strategies
  6. * @copyright (C) 2011 by iRail vzw/asbl
  7. * @license AGPLv3
  8. * @author Jan Vansteenlandt
  9. */
  10. include_once ("model/resources/strategies/ATabularData.class.php");
  11. class CSV extends ATabularData {
  12. public function __construct(){
  13. $this->parameters["uri"] = "The URI to the CSV file";
  14. $this->parameters["columns"] = "The columns that are to be published, if empty every column will be published.";
  15. $this->parameters["PK"] = "The primary key of an entry";
  16. $this->requiredParameters[] = "uri";
  17. }
  18. public function onCall($package, $resource) {
  19. /*
  20. * First retrieve the values for the generic fields of the CSV logic
  21. */
  22. $result = DBQueries::getCSVResource($package, $resource);
  23. $gen_res_id = $result["gen_res_id"];
  24. if (isset($result["uri"])) {
  25. $filename = $result["uri"];
  26. } else {
  27. throw new ResourceTDTException("Can't find URI of the CSV");
  28. }
  29. $columns = array();
  30. // get the columns from the columns table
  31. $allowed_columns = DBQueries::getPublishedColumns($gen_res_id);
  32. $PK = "";
  33. foreach ($allowed_columns as $result) {
  34. array_push($columns, $result["column_name"]);
  35. if ($result["is_primary_key"] == 1) {
  36. $PK = $result["column_name"];
  37. }
  38. }
  39. $resultobject = new stdClass();
  40. $arrayOfRowObjects = array();
  41. $row = 0;
  42. // only request public available files
  43. $request = TDT::HttpRequest($filename);
  44. if (isset($request->error)) {
  45. throw new CouldNotGetDataTDTException($filename);
  46. }
  47. $csv = utf8_encode($request->data);
  48. try {
  49. // find the delimiter
  50. $commas = substr_count($csv, ",", 0, strlen($csv)>127?127:strlen($csv));
  51. $semicolons = substr_count($csv, ";", 0, strlen($csv)>127?127:strlen($csv));
  52. $rows = str_getcsv($csv, "\n");
  53. $fieldhash = array();
  54. foreach($rows as $row => $fields) {
  55. $data = str_getcsv($fields, $commas>$semicolons?",":";");
  56. // keys not found yet
  57. if(!count($fieldhash)) {
  58. // <<fast!>> way to detect empty fields
  59. // if it contains empty fields, it should not be our field hash
  60. $empty_elements = array_keys($data,"");
  61. if(!count($empty_elements)) {
  62. // we found our key fields
  63. for($i = 0; $i < sizeof($data); $i++)
  64. $fieldhash[$data[$i]] = $i;
  65. }
  66. } else {
  67. $rowobject = new stdClass();
  68. $keys = array_keys($fieldhash);
  69. for($i = 0; $i < sizeof($keys); $i++) {
  70. $c = $keys[$i];
  71. if (sizeof($columns) == 0 || in_array($c, $columns)) {
  72. $rowobject->$c = $data[$fieldhash[$c]];
  73. }
  74. }
  75. if ($PK == "") {
  76. array_push($arrayOfRowObjects, $rowobject);
  77. } else {
  78. if (! isset($arrayOfRowObjects[$rowobject->$PK])) {
  79. $arrayOfRowObjects[$rowobject->$PK] = $rowobject;
  80. }
  81. }
  82. }
  83. }
  84. $resultobject->object = $arrayOfRowObjects;
  85. return $resultobject;
  86. } catch (Exception $ex) {
  87. throw new CouldNotGetDataTDTException($filename);
  88. }
  89. }
  90. public function onDelete($package, $resource) {
  91. DBQueries::deleteCSVResource($package, $resource);
  92. }
  93. public function onAdd($package_id, $resource_id) {
  94. $this->evaluateCSVResource($resource_id);
  95. if (!isset($this->PK))
  96. $this->PK = "";
  97. }
  98. if(!isset($this->columns)){
  99. $this->columns = "";
  100. }
  101. if ($this->columns != "") {
  102. parent::evaluateColumns($this->columns, $this->PK, $resource_id);
  103. }
  104. }
  105. private function evaluateCSVResource($resource_id) {
  106. DBQueries::storeCSVResource($resource_id, $this->uri);
  107. }
  108. }
  109. ?>