Newer
Older
mattpiwik
a validé
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
" 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
$thisIsNotThatSimple = false;
foreach($array as $columnName => $columnValue )
{
if(is_array($columnValue) || is_object($columnValue))
{
$thisIsNotThatSimple = true;
break;
}
}
if($thisIsNotThatSimple === false)
{
// 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;
}
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;
mattpiwik
a validé
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
}
}
$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);
}
}
/**
* Rewrites the input $array
* array (
* LABEL => array(col1 => X, col2 => Y),
* LABEL2 => array(col1 => X, col2 => Y),
* )
* to the structure
* array (
* array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL, col1 => X, col2 => Y)),
* array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL2, col1 => X, col2 => Y)),
* )
*
* It also works with array having only one value per row, eg.
* array (
* LABEL => X,
* LABEL2 => Y,
* )
* would be converted to the structure
* array (
* array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL, 'value' => X)),
* array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL2, 'value' => Y)),
* )
*
mattpiwik
a validé
* The optional parameter $subtablePerLabel is an array of subTable associated to the rows of the $array
* For example if $subtablePerLabel is given
* array(
* LABEL => #Piwik_DataTable_ForLABEL,
* LABEL2 => #Piwik_DataTable_ForLABEL2,
* )
*
* the $array would become
* array (
* array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL, col1 => X, col2 => Y),
* Piwik_DataTable_Row::DATATABLE_ASSOCIATED => #ID DataTable For LABEL
* ),
* array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL2, col1 => X, col2 => Y)
* Piwik_DataTable_Row::DATATABLE_ASSOCIATED => #ID2 DataTable For LABEL2
* ),
* )
*
* @param array $array See method description
* @param array|null $subtablePerLabel see method description
*/
public function addRowsFromArrayWithIndexLabel( $array, $subtablePerLabel = null)
mattpiwik
a validé
{
$cleanRow = array();
foreach($array as $label => $row)
{
if(!is_array($row))
{
$row = array('value' => $row);
}
$cleanRow[Piwik_DataTable_Row::DATATABLE_ASSOCIATED] = null;
// we put the 'label' column first as it looks prettier in API results
mattpiwik
a validé
$cleanRow[Piwik_DataTable_Row::COLUMNS] = array('label' => $label) + $row;
if(!is_null($subtablePerLabel)
// some rows of this table don't have subtables
// (for example case of campaigns without keywords)
mattpiwik
a validé
&& isset($subtablePerLabel[$label])
)
{
$cleanRow[Piwik_DataTable_Row::DATATABLE_ASSOCIATED] = $subtablePerLabel[$label];
}
$this->addRow( new Piwik_DataTable_Row($cleanRow) );
}
}
}