forked from iRail/The-DataTank
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request iRail#4 from lievenjanssen/master
Generic HTML Table Resource + small bug generic XLS Resource
Showing
6 changed files
with
283 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
/** | ||
* This class handles a XLS file | ||
* | ||
* @package The-Datatank/model/resources/strategies | ||
* @copyright (C) 2011 by iRail vzw/asbl | ||
* @license AGPLv3 | ||
* @author Lieven Janssen | ||
*/ | ||
include_once("model/resources/strategies/ATabularData.class.php"); | ||
|
||
class HTMLTable extends ATabularData { | ||
|
||
public function __construct() { | ||
if(Config::$PHPEXCEL_IOFACTORY_PATH!="") { | ||
if(!file_exists(Config::$PHPEXCEL_IOFACTORY_PATH)){ | ||
throw new NotFoundTDTException("Could not include " . Config::$PHPEXCEL_IOFACTORY_PATH); | ||
} else { | ||
include_once(Config::$PHPEXCEL_IOFACTORY_PATH); | ||
} | ||
} else { | ||
throw new NotFoundTDTException("PHPExcel path not defined in config.class"); | ||
} | ||
} | ||
|
||
public function onCall($package,$resource){ | ||
|
||
/* | ||
* First retrieve the values for the generic fields of the HTML Table logic | ||
*/ | ||
$result = DBQueries::getHTMLTableResource($package, $resource); | ||
|
||
$gen_res_id = $result["gen_res_id"]; | ||
|
||
if(isset($result["uri"])){ | ||
$uri = $result["uri"]; | ||
}else{ | ||
throw new ResourceTDTException("Can't find URI of the HTML Table"); | ||
} | ||
|
||
if(isset($result["xpath"])){ | ||
$xpath = $result["xpath"]; | ||
}else{ | ||
throw new ResourceTDTException("Can't find xpath of the HTML Table"); | ||
} | ||
|
||
$columns = array(); | ||
|
||
// get the columns from the columns table | ||
$allowed_columns = DBQueries::getPublishedColumns($gen_res_id); | ||
|
||
$columns = array(); | ||
$PK = ""; | ||
foreach($allowed_columns as $result){ | ||
array_push($columns,$result["column_name"]); | ||
if($result["is_primary_key"] == 1){ | ||
$PK = $result["column_name"]; | ||
} | ||
} | ||
|
||
$resultobject = new stdClass(); | ||
$arrayOfRowObjects = array(); | ||
$row = 0; | ||
|
||
/* | ||
if(!file_exists($uri)){ | ||
throw new CouldNotGetDataTDTException($uri); | ||
} | ||
*/ | ||
try { | ||
|
||
$oldSetting = libxml_use_internal_errors( true ); | ||
libxml_clear_errors(); | ||
|
||
$html = new DOMDocument(); | ||
$html->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"]); | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<html> | ||
<table> | ||
<thead> | ||
<tr><th>name<th>age</th><th>city</th></tr> | ||
</thead> | ||
<tbody> | ||
<tr><td>Jan<td>22</td><td>Veridian City</td></tr> | ||
<tr><td>Pieter</td><td>22</td><td>Pallet Town</td></tr> | ||
<tr><td>Ash</td><td>21</td><td>Pallet Town</td></tr> | ||
</tbody> | ||
</table> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* This class tests the generic XLS resource | ||
* | ||
* @package The-Datatank/model/resources/strategies | ||
* @copyright (C) 2011 by iRail vzw/asbl | ||
* @license AGPLv3 | ||
* @author Lieven Janssen | ||
*/ | ||
|
||
include_once(dirname(__FILE__)."/simpletest/autorun.php"); | ||
include_once(dirname(__FILE__)."/TDTUnitTest.class.php"); | ||
include_once(dirname(__FILE__)."/../classes/REST.class.php"); | ||
|
||
class APITestGenericHTMLTable extends TDTUnitTest{ | ||
|
||
//private $location = "http://www.nieuws.be/"; | ||
//private $xpath = "//table[@id='htmlGrid_a119d7e2-e979-436a-8cad-765d37fc0fdd']"; | ||
private $location = "unittests/temp/person.html"; | ||
private $xpath = "//table"; | ||
private $install_as = "htmltablepackage/person/"; | ||
private $generic_type = "HTMLTable"; | ||
private $printmethods = "html;json;xml;jsonp"; | ||
//private $columns = "Datum;Titel;Bron"; | ||
//private $PK = "Titel"; | ||
private $columns = "name;age;city"; | ||
private $PK = "name"; | ||
|
||
function testPutHTMLTable(){ | ||
|
||
$url = Config::$HOSTNAME . Config::$SUBDIR . $this->install_as; | ||
$data = array( "resource_type" => "generic", | ||
"printmethods" => $this->printmethods, | ||
"generic_type" => $this->generic_type, | ||
"documentation" => "this is some documentation.", | ||
"uri" => Config::$HOSTNAME . Config::$SUBDIR . $this->location, | ||
"xpath" => $this->xpath, | ||
"columns" => $this->columns, | ||
"PK" => $this->PK | ||
); | ||
|
||
$request = new REST($url, $data, "PUT"); | ||
$request->execute(); | ||
|
||
$this->assertEqual($request->http_code, 200); | ||
if($request->result) | ||
$this->debug($request->result); | ||
} | ||
|
||
function testGetHTMLTable() { | ||
$url = Config::$HOSTNAME . Config::$SUBDIR . $this->install_as; | ||
$request = new REST($url, array(), "GET"); | ||
$request->execute(); | ||
|
||
$this->assertEqual($request->http_code, 200); | ||
if($request->http_code != 200 && $request->result) | ||
$this->debug($request->result); | ||
} | ||
function testDeleteHTMLTable() { | ||
$url = Config::$HOSTNAME . Config::$SUBDIR . $this->install_as; | ||
$request = new REST($url, array(), "DELETE"); | ||
$request->execute(); | ||
|
||
$this->assertEqual($request->http_code, 200); | ||
if($request->result) | ||
$this->debug($request->result); | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters