renderTable($this->table); } protected function renderTable($table) { $renderer = new Piwik_DataTable_Renderer_Php($table, $serialize = false); $array = $renderer->flatRender(); // case DataTable_Array if($table instanceof Piwik_DataTable_Array) { return $this->renderDataTableArray($table, $array); } // integer value of ZERO is a value we want to display if($array != 0 && empty($array)) { $out = ""; return $this->output($out); } if($table instanceof Piwik_DataTable_Simple) { if(is_array($array)) { $out = $this->renderDataTableSimple($array); $out = "\n".$out.""; } else { $out = "".$array.""; } return $this->output($out); } if($table instanceof Piwik_DataTable) { $out = $this->renderDataTable($array); $out = "\n$out"; return $this->output($out); } } protected function renderDataTableArray($table, $array) { // CASE 1 //array // 'day1' => string '14' (length=2) // 'day2' => string '6' (length=1) $firstTable = current($array); if(!is_array( $firstTable )) { $xml = "\n"; $nameDescriptionAttribute = $table->getNameKey(); foreach($array as $valueAttribute => $value) { if(!empty($value)) { $xml .= "\t$value\n"; } else { $xml .= "\t\n"; } } $xml .= ""; return $this->output($xml); } $subTables = $table->getArray(); $firstTable = current($subTables); // CASE 2 //array // 'day1' => // array // 'nb_uniq_visitors' => string '18' // 'nb_visits' => string '101' // 'day2' => // array // 'nb_uniq_visitors' => string '28' // 'nb_visits' => string '11' if( $firstTable instanceof Piwik_DataTable_Simple) { $xml = "\n"; $nameDescriptionAttribute = $table->getNameKey(); foreach($array as $valueAttribute => $dataTableSimple) { if(count($dataTableSimple) == 0) { $xml .= "\t\n"; } else { if(is_array($dataTableSimple)) { $dataTableSimple = "\n" . $this->renderDataTableSimple($dataTableSimple, "\t") . "\t" ; } $xml .= "\t".$dataTableSimple."\n"; } } $xml .= ""; return $this->output($xml); } // CASE 3 //array // 'day1' => // array // 0 => // array // 'label' => string 'phpmyvisites' // 'nb_uniq_visitors' => int 11 // 'nb_visits' => int 13 // 1 => // array // 'label' => string 'phpmyvisits' // 'nb_uniq_visitors' => int 2 // 'nb_visits' => int 2 // 'day2' => // array // 0 => // array // 'label' => string 'piwik' // 'nb_uniq_visitors' => int 121 // 'nb_visits' => int 130 // 1 => // array // 'label' => string 'piwik bis' // 'nb_uniq_visitors' => int 20 // 'nb_visits' => int 120 if($firstTable instanceof Piwik_DataTable) { $out = "\n"; $nameDescriptionAttribute = $table->getNameKey(); foreach($array as $keyName => $arrayForSingleDate) { $dataTableOut = $this->renderDataTable( $arrayForSingleDate, "\t" ); if(empty($dataTableOut)) { $out .= "\t\n"; } else { $out .= "\t\n"; $out .= $dataTableOut; $out .= "\t\n"; } } $out .= ""; return $this->output($out); } } protected function renderDataTable( $array, $prefixLine = "" ) { // var_dump($array);exit; $out = ''; foreach($array as $row) { $out .= $prefixLine."\t\n"; foreach($row as $name => $value) { // 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\n"; } $out .= $prefixLine."\t\n"; } return $out; } protected function renderDataTableSimple( $array, $prefixLine = "") { $out = ''; foreach($array as $keyName => $value) { $out .= $prefixLine."\t<$keyName>$value\n"; } return $out; } protected function output( $xml ) { // silent fail because otherwise it throws an exception in the unit tests @header("Content-Type: text/xml;charset=utf-8"); $xml = '' . "\n" . $xml; return $xml; } }