diff --git a/model/DBQueries.class.php b/model/DBQueries.class.php index fe93d46a..665260ea 100644 --- a/model/DBQueries.class.php +++ b/model/DBQueries.class.php @@ -572,6 +572,48 @@ static function deleteXLSResource($package, $resource) { and package.id=package_id)", array(":package" => $package, ":resource" => $resource) ); + } + + /** + * Retrieve a specific HTML Table resource + */ + static function getHTMLTableResource($package, $resource) { + return R::getRow( + "SELECT generic_resource.id as gen_res_id,generic_resource_htmltable.uri as uri,generic_resource_htmltable.xpath as xpath + FROM package,resource, generic_resource, generic_resource_htmltable + WHERE package.package_name=:package and resource.resource_name=:resource + and package.id=resource.package_id + and resource.id = generic_resource.resource_id + and generic_resource.id=generic_resource_htmltable.gen_resource_id", + array(':package' => $package, ':resource' => $resource) + ); + } + + /** + * Store a HTML Table resource + */ + static function storeHTMLTableResource($resource_id, $uri, $xpath) { + $xlsresource = R::dispense("generic_resource_htmltable"); + $xlsresource->gen_resource_id = $resource_id; + $xlsresource->uri = $uri; + $xlsresource->xpath = $xpath; + return R::store($xlsresource); + } + + /** + * Delete a specific HTML Table resource + */ + static function deleteHTMLTableResource($package, $resource) { + return R::exec( + "DELETE FROM generic_resource_htmltable + WHERE gen_resource_id IN + (SELECT generic_resource.id FROM generic_resource,package,resource + WHERE resource.resource_name=:resource + and package.package_name=:package + and resource_id = resource.id + and package.id=package_id)", + array(":package" => $package, ":resource" => $resource) + ); } /** diff --git a/model/resources/strategies/HTMLTable.class.php b/model/resources/strategies/HTMLTable.class.php new file mode 100644 index 00000000..505a57df --- /dev/null +++ b/model/resources/strategies/HTMLTable.class.php @@ -0,0 +1,151 @@ +loadHtmlFile($uri); + + $domxpath = new DOMXPath( $html ); + $tablerows = $domxpath->query($xpath . "/tr" ); + if ($tablerows->length == 0) { + //table has thead and tbody + $tablerows = $domxpath->query($xpath . "/*/tr" ); + } + + $rowIndex = 1; + foreach ($tablerows as $tr) { + $newDom = new DOMDocument; + $newDom->appendChild($newDom->importNode($tr,true)); + + $domxpath = new DOMXPath( $newDom ); + if ($rowIndex == 1) { + $tablecols = $domxpath->query("td"); + if ($tablecols->length == 0) { + //thead row has th instead of td + $tablecols = $domxpath->query("th" ); + } + $columnIndex = 1; + foreach($tablecols as $td) { + $fieldhash[ $td->nodeValue ] = $columnIndex; + $columnIndex++; + } + } else { + $tablecols = $domxpath->query("td"); + $columnIndex = 1; + $rowobject = new stdClass(); + $keys = array_keys($fieldhash); + foreach($tablecols as $td) { + $c = $keys[$columnIndex - 1]; + if(sizeof($columns) == 0 || in_array($c,$columns)){ + $rowobject->$c = $td->nodeValue; + } + $columnIndex++; + } + if($PK == "") { + array_push($arrayOfRowObjects,$rowobject); + } else { + if(!isset($arrayOfRowObjects[$rowobject->$PK])){ + $arrayOfRowObjects[$rowobject->$PK] = $rowobject; + } + } + } + $rowIndex++; + } + + $resultobject->object = $arrayOfRowObjects; + return $resultobject; + } catch( Exception $ex) { + throw new CouldNotGetDataTDTException( $uri ); + } + } + + public function onDelete($package,$resource){ + DBQueries::deleteHTMLTableResource($package, $resource); + } + + public function onAdd($package_id,$resource_id,$content){ + $this->evaluateHTMLTableResource($resource_id,$content); + parent::evaluateColumns($content["columns"],$content["PK"],$resource_id); + } + + public function onUpdate($package,$resource,$content){ + // At the moment there's no request for foreign relationships between XLS files + // Yet this could be perfectly possible! + } + + + private function evaluateHTMLTableResource($resource_id,$content){ + DBQueries::storeHTMLTableResource($resource_id, $content["uri"], $content["xpath"]); + } +} +?> diff --git a/model/resources/strategies/XLS.class.php b/model/resources/strategies/XLS.class.php index 4dd92186..649826ee 100644 --- a/model/resources/strategies/XLS.class.php +++ b/model/resources/strategies/XLS.class.php @@ -91,13 +91,13 @@ public function onCall($package,$resource){ if(sizeof($columns) == 0 || in_array($c,$columns)){ $rowobject->$c = $cell->getCalculatedValue(); } - if($PK == "") { - array_push($arrayOfRowObjects,$rowobject); - } else { - if(!isset($arrayOfRowObjects[$rowobject->$PK])){ - $arrayOfRowObjects[$rowobject->$PK] = $rowobject; - } - } + } + } + if($PK == "") { + array_push($arrayOfRowObjects,$rowobject); + } else { + if(!isset($arrayOfRowObjects[$rowobject->$PK])){ + $arrayOfRowObjects[$rowobject->$PK] = $rowobject; } } } diff --git a/unittests/temp/person.html b/unittests/temp/person.html new file mode 100644 index 00000000..20e50c7b --- /dev/null +++ b/unittests/temp/person.html @@ -0,0 +1,12 @@ + +
name | age | city |
---|---|---|
Jan | 22 | Veridian City |
Pieter | 22 | Pallet Town |
Ash | 21 | Pallet Town |