Skip to content

Commit

Permalink
Merge pull request iRail#4 from lievenjanssen/master
Browse files Browse the repository at this point in the history
Generic HTML Table Resource + small bug generic XLS Resource
Pieter Colpaert committed Sep 19, 2011
2 parents 5d79cd1 + dac46f4 commit 2904ddc
Showing 6 changed files with 283 additions and 7 deletions.
42 changes: 42 additions & 0 deletions model/DBQueries.class.php
Original file line number Diff line number Diff line change
@@ -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)
);
}

/**
151 changes: 151 additions & 0 deletions model/resources/strategies/HTMLTable.class.php
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"]);
}
}
?>
14 changes: 7 additions & 7 deletions model/resources/strategies/XLS.class.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
12 changes: 12 additions & 0 deletions unittests/temp/person.html
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>
70 changes: 70 additions & 0 deletions unittests/tests/APITestGenericHTMLTable.class.php
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);
}
}

?>
1 change: 1 addition & 0 deletions unittests/tests/APITestSuite.class.php
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ function TestAPISuite() {
$this->addFile(dirname(__FILE__) . "/APITestInstalled.class.php");
$this->addFile(dirname(__FILE__) . "/APITestGenericCSV.class.php");
$this->addFile(dirname(__FILE__) . "/APITestGenericXLS.class.php");
$this->addFile(dirname(__FILE__) . "/APITestGenericHTMLTable.class.php");
$this->addFile(dirname(__FILE__) . "/APITestGenericDB.class.php");
$this->addFile(dirname(__FILE__) . "/APITestRemote.class.php");
}

0 comments on commit 2904ddc

Please sign in to comment.