Beispiel #1
0
 /**
  * Converts the output of the given simple data table
  *
  * @param DataTable|Simple $table
  * @param array $allColumns
  * @return string
  */
 protected function renderDataTable($table, &$allColumns = array())
 {
     if ($table instanceof Simple) {
         $row = $table->getFirstRow();
         if ($row !== false) {
             $columnNameToValue = $row->getColumns();
             if (count($columnNameToValue) == 1) {
                 // simple tables should only have one column, the value
                 $allColumns['value'] = true;
                 $value = array_values($columnNameToValue);
                 $str = 'value' . $this->lineEnd . $this->formatValue($value[0]);
                 return $str;
             }
         }
     }
     $csv = array();
     foreach ($table->getRows() as $row) {
         $csvRow = $this->flattenColumnArray($row->getColumns());
         if ($this->exportMetadata) {
             $metadata = $row->getMetadata();
             foreach ($metadata as $name => $value) {
                 if ($name == 'idsubdatatable_in_db') {
                     continue;
                 }
                 //if a metadata and a column have the same name make sure they dont overwrite
                 if ($this->translateColumnNames) {
                     $name = Piwik::translate('General_Metadata') . ': ' . $name;
                 } else {
                     $name = 'metadata_' . $name;
                 }
                 $csvRow[$name] = $value;
             }
         }
         foreach ($csvRow as $name => $value) {
             $allColumns[$name] = true;
         }
         if ($this->exportIdSubtable) {
             $idsubdatatable = $row->getIdSubDataTable();
             if ($idsubdatatable !== false && $this->hideIdSubDatatable === false) {
                 $csvRow['idsubdatatable'] = $idsubdatatable;
             }
         }
         $csv[] = $csvRow;
     }
     // now we make sure that all the rows in the CSV array have all the columns
     foreach ($csv as &$row) {
         foreach ($allColumns as $columnName => $true) {
             if (!isset($row[$columnName])) {
                 $row[$columnName] = '';
             }
         }
     }
     $str = '';
     // specific case, we have only one column and this column wasn't named properly (indexed by a number)
     // we don't print anything in the CSV file => an empty line
     if (sizeof($allColumns) == 1 && reset($allColumns) && !is_string(key($allColumns))) {
         $str .= '';
     } else {
         // render row names
         $str .= $this->getHeaderLine(array_keys($allColumns)) . $this->lineEnd;
     }
     // we render the CSV
     foreach ($csv as $theRow) {
         $rowStr = '';
         foreach ($allColumns as $columnName => $true) {
             $rowStr .= $this->formatValue($theRow[$columnName]) . $this->separator;
         }
         // remove the last separator
         $rowStr = substr_replace($rowStr, "", -strlen($this->separator));
         $str .= $rowStr . $this->lineEnd;
     }
     $str = substr($str, 0, -strlen($this->lineEnd));
     return $str;
 }
Beispiel #2
0
 /**
  * Converts the simple data table to an array
  *
  * @param Simple $table
  * @return array
  */
 protected function renderSimpleTable($table)
 {
     $array = array();
     $row = $table->getFirstRow();
     if ($row === false) {
         return $array;
     }
     foreach ($row->getColumns() as $columnName => $columnValue) {
         $array[$columnName] = $columnValue;
     }
     return $array;
 }
Beispiel #3
0
 /**
  * Converts the output of the given simple data table
  *
  * @param DataTable|Simple $table
  * @param array $allColumns
  * @return string
  */
 protected function renderDataTable($table, &$allColumns = array())
 {
     if ($table instanceof Simple) {
         $row = $table->getFirstRow();
         if ($row !== false) {
             $columnNameToValue = $row->getColumns();
             if (count($columnNameToValue) == 1) {
                 // simple tables should only have one column, the value
                 $allColumns['value'] = true;
                 $value = array_values($columnNameToValue);
                 $str = 'value' . $this->lineEnd . $this->formatValue($value[0]);
                 return $str;
             }
         }
     }
     $csv = $this->makeArrayFromDataTable($table, $allColumns);
     // now we make sure that all the rows in the CSV array have all the columns
     foreach ($csv as &$row) {
         foreach ($allColumns as $columnName => $true) {
             if (!isset($row[$columnName])) {
                 $row[$columnName] = '';
             }
         }
     }
     $str = $this->buildCsvString($allColumns, $csv);
     return $str;
 }