/model/resources/strategies/XLS.class.php

https://github.com/lievenjanssen/The-DataTank · PHP · 141 lines · 111 code · 18 blank · 12 comment · 24 complexity · 20ec114d21176042acdf65794cb9ccb6 MD5 · raw file

  1. <?php
  2. /**
  3. * This class handles a XLS file
  4. *
  5. * @package The-Datatank/model/resources/strategies
  6. * @copyright (C) 2011 by iRail vzw/asbl
  7. * @license AGPLv3
  8. * @author Lieven Janssen
  9. */
  10. include_once("model/resources/strategies/ATabularData.class.php");
  11. class XLS extends ATabularData {
  12. public function __construct() {
  13. if(Config::$PHPEXCEL_IOFACTORY_PATH!="") {
  14. if(!file_exists(Config::$PHPEXCEL_IOFACTORY_PATH)){
  15. throw new NotFoundTDTException("Could not include " . Config::$PHPEXCEL_IOFACTORY_PATH);
  16. } else {
  17. include_once(Config::$PHPEXCEL_IOFACTORY_PATH);
  18. }
  19. } else {
  20. throw new NotFoundTDTException("PHPExcel path not defined in config.class");
  21. }
  22. $this->parameters["url"] = "The path to the excel sheet (can be a url as well).";
  23. $this->parameters["sheet"] = "The sheet name of the excel";
  24. $this->parameters["columns"] = "The columns that are to be published.";
  25. $this->parameters["PK"] = "The primary key for each row.";
  26. $this->requiredParameters = array_merge($this->requiredParameters, array_keys($this->parameters));
  27. }
  28. public function onCall($package,$resource){
  29. /*
  30. * First retrieve the values for the generic fields of the XLS logic
  31. */
  32. $result = DBQueries::getXLSResource($package, $resource);
  33. $gen_res_id = $result["gen_res_id"];
  34. if(isset($result["url"])){
  35. $url = $result["url"];
  36. }else{
  37. throw new ResourceTDTException("Can't find url of the XLS");
  38. }
  39. if(isset($result["sheet"])){
  40. $sheet = $result["sheet"];
  41. }else{
  42. throw new ResourceTDTException("Can't find sheet of the XLS");
  43. }
  44. $columns = array();
  45. // get the columns from the columns table
  46. $allowed_columns = DBQueries::getPublishedColumns($gen_res_id);
  47. $columns = array();
  48. $PK = "";
  49. foreach($allowed_columns as $result){
  50. array_push($columns,$result["column_name"]);
  51. if($result["is_primary_key"] == 1){
  52. $PK = $result["column_name"];
  53. }
  54. }
  55. $resultobject = new stdClass();
  56. $arrayOfRowObjects = array();
  57. $row = 0;
  58. if(!file_exists($url)){
  59. throw new CouldNotGetDataTDTException($url);
  60. }
  61. try {
  62. $objReader = PHPExcel_IOFactory::createReader('Excel2007');
  63. $objReader->setLoadSheetsOnly($sheet);
  64. $objPHPExcel = $objReader->load($url);
  65. $worksheet = $objPHPExcel->getSheetByName($sheet);
  66. foreach ($worksheet->getRowIterator() as $row) {
  67. $rowIndex = $row->getRowIndex();
  68. $cellIterator = $row->getCellIterator();
  69. $cellIterator->setIterateOnlyExistingCells(false);
  70. if ($rowIndex == 1) {
  71. foreach ($cellIterator as $cell) {
  72. $columnIndex = $cell->columnIndexFromString($cell->getColumn());
  73. $fieldhash[ $cell->getCalculatedValue() ] = $columnIndex;
  74. }
  75. }
  76. else {
  77. $rowobject = new stdClass();
  78. $keys = array_keys($fieldhash);
  79. foreach ($cellIterator as $cell) {
  80. $columnIndex = $cell->columnIndexFromString($cell->getColumn());
  81. if (!is_null($cell)) {
  82. $c = $keys[$columnIndex - 1];
  83. if(sizeof($columns) == 0 || in_array($c,$columns)){
  84. $rowobject->$c = $cell->getCalculatedValue();
  85. }
  86. }
  87. }
  88. if($PK == "") {
  89. array_push($arrayOfRowObjects,$rowobject);
  90. } else {
  91. if(!isset($arrayOfRowObjects[$rowobject->$PK])){
  92. $arrayOfRowObjects[$rowobject->$PK] = $rowobject;
  93. }
  94. }
  95. }
  96. }
  97. $resultobject->object = $arrayOfRowObjects;
  98. return $resultobject;
  99. } catch( Exception $ex) {
  100. throw new CouldNotGetDataTDTException( $url );
  101. }
  102. }
  103. public function onDelete($package,$resource){
  104. DBQueries::deleteXLSResource($package, $resource);
  105. }
  106. public function onAdd($package_id,$resource_id){
  107. $this->evaluateXLSResource($resource_id);
  108. if (!isset($this->PK))
  109. $this->PK = "";
  110. }
  111. if(!isset($this->columns)){
  112. $this->columns = "";
  113. }
  114. if ($this->columns != "") {
  115. parent::evaluateColumns($this->columns, $this->PK, $resource_id);
  116. }
  117. }
  118. private function evaluateXLSResource($resource_id){
  119. DBQueries::storeXLSResource($resource_id, $this->url, $this->sheet);
  120. }
  121. }
  122. ?>