diff options
Diffstat (limited to 'modules/DataTable.php')
-rw-r--r-- | modules/DataTable.php | 254 |
1 files changed, 127 insertions, 127 deletions
diff --git a/modules/DataTable.php b/modules/DataTable.php index 7321bfd3b8..3a47ea2554 100644 --- a/modules/DataTable.php +++ b/modules/DataTable.php @@ -1,19 +1,19 @@ -<?php
-/**
- * Piwik - Open source web analytics
- *
- * @link http://piwik.org
- * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
- * @version $Id$
- *
- * @package Piwik_DataTable
- */
-
-require_once "DataTable/Renderer.php";
-require_once "DataTable/Renderer/Console.php";
-require_once "DataTable/Filter.php";
-require_once "DataTable/Row.php";
-require_once "DataTable/Manager.php";
+<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later + * @version $Id$ + * + * @package Piwik_DataTable + */ + +require_once "DataTable/Renderer.php"; +require_once "DataTable/Renderer/Console.php"; +require_once "DataTable/Filter.php"; +require_once "DataTable/Row.php"; +require_once "DataTable/Manager.php"; /** * * Initial Specification @@ -395,28 +395,28 @@ class Piwik_DataTable { return $this->currentId; } -
+ /** - * Adds a new row from a PHP array data structure
+ * Adds a new row from a PHP array data structure * You should use loadFromArray for performance! * - * @param array $row, eg. array(Piwik_DataTable_Row::COLUMNS => array( 'visits' => 13, 'test' => 'toto'),)
- */
- public function addRowFromArray( $row )
- {
- $this->loadFromArray(array($row));
- }
-
- /** - * Adds a new row a PHP array data structure
+ * @param array $row, eg. array(Piwik_DataTable_Row::COLUMNS => array( 'visits' => 13, 'test' => 'toto'),) + */ + public function addRowFromArray( $row ) + { + $this->loadFromArray(array($row)); + } + + /** + * Adds a new row a PHP array data structure * You should use loadFromSimpleArray for performance! * - * @param array $row, eg. array('name' => 'google analytics', 'license' => 'commercial')
- */
- public function addRowFromSimpleArray( $row )
- {
- $this->loadFromSimpleArray(array($row));
- }
+ * @param array $row, eg. array('name' => 'google analytics', 'license' => 'commercial') + */ + public function addRowFromSimpleArray( $row ) + { + $this->loadFromSimpleArray(array($row)); + } /** * Returns the array of Piwik_DataTable_Row @@ -623,7 +623,7 @@ class Piwik_DataTable if($depth > self::MAXIMUM_DEPTH_LEVEL_ALLOWED) { throw new Exception("Maximum recursion level of ".self::MAXIMUM_DEPTH_LEVEL_ALLOWED. " reached. You have probably set a DataTable_Row with an associated DataTable which belongs already to its parent hierarchy."); - }
+ } // For each row, get the serialized row // If it is associated to a sub table, get the serialized table recursively ; @@ -642,14 +642,14 @@ class Piwik_DataTable } } // we load the current Id of the DataTable - $forcedId = $this->getId();
-
+ $forcedId = $this->getId(); + // if the datatable is the parent we force the Id at 0 (this is part of the specification) if($depth==0) { $forcedId = 0; - }
-
+ } + // we then serialize the rows and store them in the serialized dataTable $aSerializedDataTable[$forcedId] = serialize($this->rows); @@ -681,14 +681,14 @@ class Piwik_DataTable * Loads the DataTable from a PHP array data structure * * @param array Array with the following structure - * array(
+ * array( * // row1 - * array(
- * Piwik_DataTable_Row::COLUMNS => array( col1_name => value1, col2_name => value2, ...),
- * Piwik_DataTable_Row::DETAILS => array( detail1_name => value1, ...), // see Piwik_DataTable_Row
- *
- * ),
- *
+ * array( + * Piwik_DataTable_Row::COLUMNS => array( col1_name => value1, col2_name => value2, ...), + * Piwik_DataTable_Row::DETAILS => array( detail1_name => value1, ...), // see Piwik_DataTable_Row + * + * ), + * * // row2 * array( ... ), * @@ -706,90 +706,90 @@ class Piwik_DataTable $this->addRow($row); } - }
-
- /**
- * Loads the data from a simple php array.
- * Basically maps a simple multidimensional php array to a DataTable.
- * Not recursive (if a row contains a php array itself, it won't be loaded)
- *
- * @param array Array with the simple structure:
- * array(
- * array( col1_name => valueA, col2_name => valueC, ...),
- * array( col1_name => valueB, col2_name => valueD, ...),
- * )
- */
- public function loadFromSimpleArray( $array )
- {
- // we define an exception we may throw if at one point we notice that we cannot handle the data structure
- $e = new Exception(" Data structure returned is not convertible in the requested format.".
- " Try to call this method with the parameters '&format=original&serialize=1'".
- "; you will get the original php data structure serialized.".
- " The data structure looks like this: \n \$data = " . var_export($array, true) . "; ");
-
-
- // first pass to see if the array has the structure
- // 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
+ } + + /** + * Loads the data from a simple php array. + * Basically maps a simple multidimensional php array to a DataTable. + * Not recursive (if a row contains a php array itself, it won't be loaded) + * + * @param array Array with the simple structure: + * array( + * array( col1_name => valueA, col2_name => valueC, ...), + * array( col1_name => valueB, col2_name => valueD, ...), + * ) + */ + public function loadFromSimpleArray( $array ) + { + // we define an exception we may throw if at one point we notice that we cannot handle the data structure + $e = new Exception(" Data structure returned is not convertible in the requested format.". + " Try to call this method with the parameters '&format=original&serialize=1'". + "; you will get the original php data structure serialized.". + " The data structure looks like this: \n \$data = " . var_export($array, true) . "; "); + + + // first pass to see if the array has the structure + // 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; -
- foreach($array as $columnName => $columnValue )
- {
- if(is_array($columnValue) || is_object($columnValue))
- {
- $thisIsNotThatSimple = true;
- break;
- }
- $rowBuilt += array($columnName => $columnValue );
+ + foreach($array as $columnName => $columnValue ) + { + if(is_array($columnValue) || is_object($columnValue)) + { + $thisIsNotThatSimple = true; + break; + } + $rowBuilt += array($columnName => $columnValue ); + } + + if($thisIsNotThatSimple === false) + { + $this->addRow( new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => $rowBuilt ) ) ); + // we have converted our simple array to one single row + // => we exit the method as the job is now finished + return; + } + + + foreach($array as $key => $row) + { + // stuff that looks like a line + if(is_array($row)) + { + /** + * We make sure we can convert this PHP array without losing information. + * We are able to convert only simple php array (no strings keys, no sub arrays, etc.) + * + */ + + // if the key is a string it means that some information was contained in this key. + // it cannot be lost during the conversion. Because we are not able to handle properly + // this key, we throw an explicit exception. + if(is_string($key)) + { + throw $e; + } + // if any of the sub elements of row is an array we cannot handle this data structure... + foreach($row as $subRow) + { + if(is_array($subRow)) + { + throw $e; + } + } + + + $row = new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => $row ) ); + } + // other (string, numbers...) => we build a line from this value + else + { + $row = new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => array($key => $row)) ); + } + $this->addRow($row); } -
- if($thisIsNotThatSimple === false)
- {
- $this->addRow( new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => $rowBuilt ) ) );
- // we have converted our simple array to one single row
- // => we exit the method as the job is now finished
- return;
- }
-
-
- foreach($array as $key => $row)
- {
- // stuff that looks like a line
- if(is_array($row))
- {
- /**
- * We make sure we can convert this PHP array without losing information.
- * We are able to convert only simple php array (no strings keys, no sub arrays, etc.)
- *
- */
-
- // if the key is a string it means that some information was contained in this key.
- // it cannot be lost during the conversion. Because we are not able to handle properly
- // this key, we throw an explicit exception.
- if(is_string($key))
- {
- throw $e;
- }
- // if any of the sub elements of row is an array we cannot handle this data structure...
- foreach($row as $subRow)
- {
- if(is_array($subRow))
- {
- throw $e;
- }
- }
-
-
- $row = new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => $row ) );
- }
- // other (string, numbers...) => we build a line from this value
- else
- {
- $row = new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => array($key => $row)) );
- }
- $this->addRow($row);
- }
} /** |