Esempio n. 1
0
 /**
  * Computes the output of the given data table array
  *
  * @param DataTable\Map $table
  * @param array $allColumns
  * @return string
  */
 protected function renderDataTableMap($table, &$allColumns = array())
 {
     $str = '';
     foreach ($table->getDataTables() as $currentLinePrefix => $dataTable) {
         $returned = explode("\n", $this->renderTable($dataTable, $allColumns));
         // get rid of the columns names
         $returned = array_slice($returned, 1);
         // case empty datatable we dont print anything in the CSV export
         // when in xml we would output <result date="2008-01-15" />
         if (!empty($returned)) {
             foreach ($returned as &$row) {
                 $row = $currentLinePrefix . $this->separator . $row;
             }
             $str .= "\n" . implode("\n", $returned);
         }
     }
     // prepend table key to column list
     $allColumns = array_merge(array($table->getKeyName() => true), $allColumns);
     // add header to output string
     $str = $this->getHeaderLine(array_keys($allColumns)) . $str;
     return $str;
 }
Esempio n. 2
0
 /**
  * Computes the output for the given data table array
  *
  * @param Map $table
  * @param array $array
  * @param string $prefixLines
  * @return string
  */
 protected function renderDataTableMap($table, $array, $prefixLines = "")
 {
     // CASE 1
     //array
     //  'day1' => string '14' (length=2)
     //  'day2' => string '6' (length=1)
     $firstTable = current($array);
     if (!is_array($firstTable)) {
         $xml = '';
         $nameDescriptionAttribute = $table->getKeyName();
         foreach ($array as $valueAttribute => $value) {
             if (empty($value)) {
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\" />\n";
             } elseif ($value instanceof Map) {
                 $out = $this->renderTable($value, true);
                 //TODO somehow this code is not tested, cover this case
                 $xml .= "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\">\n{$out}</result>\n";
             } else {
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\">" . self::formatValueXml($value) . "</result>\n";
             }
         }
         return $xml;
     }
     $subTables = $table->getDataTables();
     $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 Simple) {
         $xml = '';
         $nameDescriptionAttribute = $table->getKeyName();
         foreach ($array as $valueAttribute => $dataTableSimple) {
             if (count($dataTableSimple) == 0) {
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\" />\n";
             } else {
                 if (is_array($dataTableSimple)) {
                     $dataTableSimple = "\n" . $this->renderDataTableSimple($dataTableSimple, $prefixLines . "\t") . $prefixLines . "\t";
                 }
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\">" . $dataTableSimple . "</result>\n";
             }
         }
         return $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 DataTable) {
         $xml = '';
         $nameDescriptionAttribute = $table->getKeyName();
         foreach ($array as $keyName => $arrayForSingleDate) {
             $dataTableOut = $this->renderDataTable($arrayForSingleDate, $prefixLines . "\t");
             if (empty($dataTableOut)) {
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$keyName}\" />\n";
             } else {
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$keyName}\">\n";
                 $xml .= $dataTableOut;
                 $xml .= $prefixLines . "\t</result>\n";
             }
         }
         return $xml;
     }
     if ($firstTable instanceof Map) {
         $xml = '';
         $tables = $table->getDataTables();
         $nameDescriptionAttribute = $table->getKeyName();
         foreach ($tables as $valueAttribute => $tableInArray) {
             $out = $this->renderTable($tableInArray, true, $prefixLines . "\t");
             $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\">\n" . $out . $prefixLines . "\t</result>\n";
         }
         return $xml;
     }
     return '';
 }