/** * This looks very similar to LabelFilter.php should it be refactored somehow? FIXME */ protected function doFilterPageDatatableSearch($callBackParameters, $table, $searchTree) { // filter a data table array if ($table instanceof DataTable\Map) { foreach ($table->getDataTables() as $subTable) { $filteredSubTable = $this->doFilterPageDatatableSearch($callBackParameters, $subTable, $searchTree); if ($filteredSubTable->getRowsCount() > 0) { // match found in a sub table, return and stop searching the others return $filteredSubTable; } } // nothing found in all sub tables return new DataTable(); } // filter regular data table if ($table instanceof DataTable) { // search for the first part of the tree search $search = array_shift($searchTree); $row = $table->getRowFromLabel($search); if ($row === false) { // not found $result = new DataTable(); $result->setAllTableMetadata($table->getAllTableMetadata()); return $result; } // end of tree search reached if (count($searchTree) == 0) { $result = new DataTable(); $result->addRow($row); $result->setAllTableMetadata($table->getAllTableMetadata()); return $result; } // match found on this level and more levels remaining: go deeper $idSubTable = $row->getIdSubDataTable(); $callBackParameters[6] = $idSubTable; /** * @var \Piwik\Period $period */ $period = $table->getMetadata('period'); if (!empty($period)) { $callBackParameters[3] = $period->getDateStart() . ',' . $period->getDateEnd(); } $table = call_user_func_array(array($this, 'getDataTableFromArchive'), $callBackParameters); return $this->doFilterPageDatatableSearch($callBackParameters, $table, $searchTree); } throw new Exception("For this API function, DataTable " . get_class($table) . " is not supported"); }
/** * Returns a new DataTable in which the rows of this table are replaced with the aggregatated rows of all its subtables. * * @param string|bool $labelColumn If supplied the label of the parent row will be added to * a new column in each subtable row. * * If set to, `'label'` each subtable row's label will be prepended * w/ the parent row's label. So `'child_label'` becomes * `'parent_label - child_label'`. * @param bool $useMetadataColumn If true and if `$labelColumn` is supplied, the parent row's * label will be added as metadata and not a new column. * @return \Piwik\DataTable */ public function mergeSubtables($labelColumn = false, $useMetadataColumn = false) { $result = new DataTable(); $result->setAllTableMetadata($this->getAllTableMetadata()); foreach ($this->getRowsWithoutSummaryRow() as $row) { $subtable = $row->getSubtable(); if ($subtable !== false) { $parentLabel = $row->getColumn('label'); // add a copy of each subtable row to the new datatable foreach ($subtable->getRows() as $id => $subRow) { $copy = clone $subRow; // if the summary row, add it to the existing summary row (or add a new one) if ($id == self::ID_SUMMARY_ROW) { $existing = $result->getRowFromId(self::ID_SUMMARY_ROW); if ($existing === false) { $result->addSummaryRow($copy); } else { $existing->sumRow($copy, $copyMeta = true, $this->getMetadata(self::COLUMN_AGGREGATION_OPS_METADATA_NAME)); } } else { if ($labelColumn !== false) { // if we're modifying the subtable's rows' label column, then we make // sure to prepend the existing label w/ the parent row's label. otherwise // we're just adding the parent row's label as a new column/metadata. $newLabel = $parentLabel; if ($labelColumn == 'label') { $newLabel .= ' - ' . $copy->getColumn('label'); } // modify the child row's label or add new column/metadata if ($useMetadataColumn) { $copy->setMetadata($labelColumn, $newLabel); } else { $copy->setColumn($labelColumn, $newLabel); } } $result->addRow($copy); } } } } return $result; }
private function makeMergedWithSiteIndex($index, $useSimpleDataTable, $isNumeric) { if ($useSimpleDataTable) { $table = new DataTable\Simple(); } else { $table = new DataTable(); } $table->setAllTableMetadata(array(DataTableFactory::TABLE_METADATA_PERIOD_INDEX => reset($this->periods))); foreach ($index as $idsite => $row) { if (!empty($row)) { $table->addRow(new Row(array(Row::COLUMNS => $row, Row::METADATA => array('idsite' => $idsite)))); } elseif ($isNumeric) { $table->addRow(new Row(array(Row::COLUMNS => $this->defaultRow, Row::METADATA => array('idsite' => $idsite)))); } } return $table; }