[Piwik-svn] r472 - in trunk/modules: . DataTable/Renderer

svnmaster at piwik.org svnmaster at piwik.org
Fri May 9 02:36:28 CEST 2008


Author: matt
Date: 2008-05-09 02:36:27 +0200 (Fri, 09 May 2008)
New Revision: 472

Modified:
   trunk/modules/DataTable.php
   trunk/modules/DataTable/Renderer/Csv.php
   trunk/modules/DataTable/Renderer/Html.php
   trunk/modules/DataTable/Renderer/Xml.php
Log:
- improving output of simple php arrays though the API renderer

Modified: trunk/modules/DataTable/Renderer/Csv.php
===================================================================
--- trunk/modules/DataTable/Renderer/Csv.php	2008-05-08 23:27:10 UTC (rev 471)
+++ trunk/modules/DataTable/Renderer/Csv.php	2008-05-09 00:36:27 UTC (rev 472)
@@ -114,7 +114,7 @@
 	protected function renderDataTable( $table )
 	{	
 		if($table instanceof Piwik_DataTable_Simple 
-			&& $table->getRowsCount() ==1)
+			&& $table->getRowsCount() == 1)
 		{
 			$str = 'value' . $this->lineEnd . $table->getRowFromId(0)->getColumn('value');
 			return $str;
@@ -122,14 +122,11 @@
 		
 		$csv = array();		
 
-		// keep track of all the existing columns in the csv file
 		$allColumns = array();
-		
 		foreach($table->getRows() as $row)
 		{
 			$csvRow = array();
 			
-			// COLUMNS
 			$columns = $row->getColumns();
 			foreach($columns as $name => $value)
 			{
@@ -142,7 +139,6 @@
 			
 			if($this->exportDetail)
 			{
-				// DETAILS
 				$details = $row->getDetails();
 				foreach($details as $name => $value)
 				{
@@ -156,7 +152,6 @@
 			
 			if($this->exportIdSubtable)
 			{
-				// ID SUB DATATABLE
 				$idsubdatatable = $row->getIdSubDataTable();
 				if($idsubdatatable !== false)
 				{
@@ -178,7 +173,6 @@
 				}
 			}
 		}
-//		var_dump($csv);exit;
 		$str = '';		
 		
 		// specific case, we have only one column and this column wasn't named properly (indexed by a number)
@@ -193,6 +187,7 @@
 		{
 			$keys = array_keys($allColumns);
 			$str .= implode($this->separator, $keys);
+			$str .= $this->lineEnd;
 		}
 		
 		// we render the CSV
@@ -205,11 +200,12 @@
 			}
 			// remove the last separator
 			$rowStr = substr_replace($rowStr,"",-strlen($this->separator));
-			
-			$str .= $this->lineEnd . $rowStr;
+			$str .= $rowStr . $this->lineEnd;
 		}
+		$str = substr($str, 0, -strlen($this->lineEnd));
 		return $str;
 	}
+	
 	protected function output( $str )
 	{
 		if(empty($str))

Modified: trunk/modules/DataTable/Renderer/Html.php
===================================================================
--- trunk/modules/DataTable/Renderer/Html.php	2008-05-08 23:27:10 UTC (rev 471)
+++ trunk/modules/DataTable/Renderer/Html.php	2008-05-09 00:36:27 UTC (rev 472)
@@ -140,6 +140,10 @@
 		{
 			if($toDisplay !== false)
 			{
+				if($name === 0)
+				{
+					$name = 'value';
+				}
 				$html .= "\n\t<td><b>$name</b></td>";
 			}
 		}

Modified: trunk/modules/DataTable/Renderer/Xml.php
===================================================================
--- trunk/modules/DataTable/Renderer/Xml.php	2008-05-08 23:27:10 UTC (rev 471)
+++ trunk/modules/DataTable/Renderer/Xml.php	2008-05-09 00:36:27 UTC (rev 472)
@@ -189,26 +189,37 @@
 	
 	protected function renderDataTable( $array, $prefixLine = "" )
 	{
-//		var_dump($array);exit;
-		
 		$out = '';
 		foreach($array as $row)
 		{
-			$out .= $prefixLine."\t<row>\n";
-			foreach($row as $name => $value)
+			$out .= $prefixLine."\t<row>";
+			
+			if(count($row) === 1
+				&& key($row) === 0)
 			{
-				// handle the recursive dataTable case by XML outputting the recursive table
-				if(is_array($value))
+				$value = current($row);
+				$out .= $prefixLine . $value;				
+			}
+			else
+			{
+				$out .= "\n";
+				foreach($row as $name => $value)
 				{
-					$value = "\n".$this->renderDataTable($value, $prefixLine."\t\t");
-					$value .= $prefixLine."\t\t"; 
-				}
-				$out .= $prefixLine."\t\t<$name>$value</$name>\n";
-			} 
-			$out .= $prefixLine."\t</row>\n";
+					// handle the recursive dataTable case by XML outputting the recursive table
+					if(is_array($value))
+					{
+						$value = "\n".$this->renderDataTable($value, $prefixLine."\t\t");
+						$value .= $prefixLine."\t\t"; 
+					}
+					$out .= $prefixLine."\t\t<$name>$value</$name>\n";
+				} 
+				$out .= "\t";
+			}
+			$out .= $prefixLine."</row>\n";
 		}
 		return $out;
 	}
+	
 	protected function renderDataTableSimple( $array, $prefixLine = "")
 	{
 		$out = '';
@@ -218,6 +229,7 @@
 		}
 		return $out;
 	}
+	
 	protected function output( $xml )
 	{
 		// silent fail because otherwise it throws an exception in the unit tests

Modified: trunk/modules/DataTable.php
===================================================================
--- trunk/modules/DataTable.php	2008-05-08 23:27:10 UTC (rev 471)
+++ trunk/modules/DataTable.php	2008-05-09 00:36:27 UTC (rev 472)
@@ -732,7 +732,7 @@
 		// array(col1_name => val1, col2_name => val2, etc.)
 		// with val* that are never arrays (only strings/numbers/bool/etc.)
 		// if we detect such a "simple" data structure we convert it to a row with the correct columns' names
-		$rowBuilt = array(); $thisIsNotThatSimple = false;
+		$thisIsNotThatSimple = false;
 		
 		foreach($array as $columnName => $columnValue )
 		{
@@ -741,12 +741,21 @@
 				$thisIsNotThatSimple = true;
 				break;
 			}
-			$rowBuilt += array($columnName => $columnValue );
 		}
-		
 		if($thisIsNotThatSimple === false)
 		{
-			$this->addRow( new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => $rowBuilt ) ) );
+			// case when the array is indexed by the default numeric index
+			if( array_keys($array) == array_keys(array_fill(0, count($array), true)) )
+			{
+				foreach($array as $row)
+				{
+					$this->addRow( new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => array($row) ) ) );					
+				}
+			}
+			else
+			{
+				$this->addRow( new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => $array ) ) );
+			}
 			// we have converted our simple array to one single row
 			// => we exit the method as the job is now finished 
 			return;
@@ -779,8 +788,6 @@
 						throw $e;						
 					}
 				}
-				
-				
 				$row = new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => $row ) );		
 			}
 			// other (string, numbers...) => we build a line from this value



More information about the Piwik-svn mailing list