addColumn() public method

Add a new column to the row. If the column already exists, throws an exception.
public addColumn ( string $name, mixed $value )
$name string name of the column to add.
$value mixed value of the column to set or a PHP callable.
Example #1
0
 public function test_getColumn_shouldPassRowToCallable()
 {
     $callbackRow = null;
     $this->row->addColumn('testClosure', function (Row $row) use(&$callbackRow) {
         $callbackRow = $row;
         return $row;
     });
     $returnedRow = $this->row->getColumn('testClosure');
     $this->assertNotEmpty($callbackRow);
     $this->assertSame($returnedRow, $callbackRow);
 }
 private function tryToAddColumn(Row $row, $column, $callable)
 {
     try {
         $row->addColumn($column, $callable);
     } catch (\Exception $e) {
     }
 }
 /** Get row evolution for a multiple labels */
 private function getMultiRowEvolution(DataTable\Map $dataTable, $metadata, $apiModule, $apiAction, $labels, $column, $legendAppendMetric = true, $labelUseAbsoluteUrl = true)
 {
     if (!isset($metadata['metrics'][$column])) {
         // invalid column => use the first one that's available
         $metrics = array_keys($metadata['metrics']);
         $column = reset($metrics);
     }
     // get the processed label and logo (if any) for every requested label
     $actualLabels = $logos = array();
     foreach ($labels as $labelIdx => $label) {
         foreach ($dataTable->getDataTables() as $table) {
             $labelRow = $this->getRowEvolutionRowFromLabelIdx($table, $labelIdx);
             if ($labelRow) {
                 $actualLabels[$labelIdx] = $this->getRowUrlForEvolutionLabel($labelRow, $apiModule, $apiAction, $labelUseAbsoluteUrl);
                 $logos[$labelIdx] = $labelRow->getMetadata('logo');
                 if (!empty($actualLabels[$labelIdx])) {
                     break;
                 }
             }
         }
         if (empty($actualLabels[$labelIdx])) {
             $actualLabels[$labelIdx] = $this->cleanOriginalLabel($label);
         }
     }
     // convert rows to be array($column.'_'.$labelIdx => $value) as opposed to
     // array('label' => $label, 'column' => $value).
     $dataTableMulti = $dataTable->getEmptyClone();
     foreach ($dataTable->getDataTables() as $tableLabel => $table) {
         $newRow = new Row();
         foreach ($labels as $labelIdx => $label) {
             $row = $this->getRowEvolutionRowFromLabelIdx($table, $labelIdx);
             $value = 0;
             if ($row) {
                 $value = $row->getColumn($column);
                 $value = floatVal(str_replace(',', '.', $value));
             }
             if ($value == '') {
                 $value = 0;
             }
             $newLabel = $column . '_' . (int) $labelIdx;
             $newRow->addColumn($newLabel, $value);
         }
         $newTable = $table->getEmptyClone();
         if (!empty($labels)) {
             // only add a row if the row has data (no labels === no data)
             $newTable->addRow($newRow);
         }
         $dataTableMulti->addTable($newTable, $tableLabel);
     }
     // the available metrics for the report are returned as metadata / columns
     $metadata['columns'] = $metadata['metrics'];
     // metadata / metrics should document the rows that are compared
     // this way, UI code can be reused
     $metadata['metrics'] = array();
     foreach ($actualLabels as $labelIndex => $label) {
         if ($legendAppendMetric) {
             $label .= ' (' . $metadata['columns'][$column] . ')';
         }
         $metricName = $column . '_' . $labelIndex;
         $metadata['metrics'][$metricName] = SafeDecodeLabel::decodeLabelSafe($label);
         if (!empty($logos[$labelIndex])) {
             $metadata['logos'][$metricName] = $logos[$labelIndex];
         }
     }
     $this->enhanceRowEvolutionMetaData($metadata, $dataTableMulti);
     return array('column' => $column, 'reportData' => $dataTableMulti, 'metadata' => $metadata);
 }
 private function addColumn(Row $row, $columnName, $callback)
 {
     $this->expectedColumns[$columnName] = true;
     $row->addColumn($columnName, $callback);
 }
Example #5
0
 /**
  * Convert a dimension-less report to a multi-row two-column data table
  *
  * @static
  * @param  $reportMetadata array
  * @param  $report DataTable
  * @param  $reportColumns array
  * @return array DataTable $report & array $columns
  */
 protected static function processTableFormat($reportMetadata, $report, $reportColumns)
 {
     $finalReport = $report;
     if (empty($reportMetadata['dimension'])) {
         $simpleReportMetrics = $report->getFirstRow();
         if ($simpleReportMetrics) {
             $finalReport = new Simple();
             foreach ($simpleReportMetrics->getColumns() as $metricId => $metric) {
                 $newRow = new Row();
                 $newRow->addColumn("label", $reportColumns[$metricId]);
                 $newRow->addColumn("value", $metric);
                 $finalReport->addRow($newRow);
             }
         }
         $reportColumns = array('label' => Piwik::translate('General_Name'), 'value' => Piwik::translate('General_Value'));
     }
     return array($finalReport, $reportColumns);
 }