Returns a row by ID. The ID is either the index of the row or {@link ID_SUMMARY_ROW}.
public getRowFromId ( integer $id ) : |
||
$id | integer | The row ID. |
return | The Row or false if not found. |
/** * Another example method that returns a data table. * @param int $idSite * @param string $period * @param string $date * @param bool|string $segment * @return DataTable */ public function getClusterSummary($idSite, $period, $date, $cluster_id, $cluster_type = 'bundle') { $table = new DataTable(); $params = array('idSite' => $idSite, 'period' => $period, 'date' => $date, 'segment' => 'customVariablePageName2==' . $cluster_type . 's;customVariablePageValue2=@' . $cluster_id); $data = \Piwik\API\Request::processRequest('API.get', $params); $tarray = $this->getTypes($params); $data->getRowFromId(0)->addColumns($tarray); $table->addRow($data->getRowFromId(0)); // Get country ISO2 code $hr_url = 'https://www.humanitarianresponse.info/api/v1.0/' . $cluster_type . 's/' . $cluster_id; if ($space_raw = @file_get_contents($hr_url)) { $space = json_decode($space_raw); $table->getRowFromId(0)->addColumn('label', $space->data[0]->label); if (isset($space->data[0]->operation[0]->country)) { $iso2 = $space->data[0]->operation[0]->country->pcode; $cparams = $params; $cparams['segment'] = $params['segment'] . ';countryCode==' . $iso2; $cdata = \Piwik\API\Request::processRequest('API.get', $cparams); $cdata->getRowFromId(0)->addColumn('label', $space->data[0]->label . ' - in country'); $ctarray = $this->getTypes($cparams); $cdata->getRowFromId(0)->addColumns($ctarray); $table->addRow($cdata->getRowFromId(0)); } } return $table; }
/** * Sums a tables row with this one. * * @param DataTable $table */ private function sumTable($table) { $metadata = $table->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME); $enableCopyMetadata = false; foreach ($table->getRowsWithoutSummaryRow() as $row) { $this->sumRow($row, $enableCopyMetadata, $metadata); } $summaryRow = $table->getRowFromId(DataTable::ID_SUMMARY_ROW); if ($summaryRow) { $this->sumRow($summaryRow, $enableCopyMetadata, $metadata); } }
/** * @param DataTable $table */ public function filter($table) { $idSubtable = $this->idSubtable ?: $table->getId(); $subTableRow = $this->firstLevelSearchEnginesTable->getRowFromIdSubDataTable($idSubtable); if (!empty($subTableRow)) { $searchEngineUrl = $subTableRow->getMetadata('url'); $table->queueFilter('ColumnCallbackAddMetadata', array('label', 'url', 'Piwik\\Plugins\\Referrers\\getSearchEngineUrlFromKeywordAndUrl', array($searchEngineUrl))); $table->queueFilter(function (DataTable $table) { $row = $table->getRowFromId(DataTable::ID_SUMMARY_ROW); if ($row) { $row->deleteMetadata('url'); } }); } $table->queueFilter('Piwik\\Plugins\\Referrers\\DataTable\\Filter\\KeywordNotDefined'); }
/** * See {@link Limit}. * * @param DataTable $table */ public function filter($table) { $table->setMetadata(DataTable::TOTAL_ROWS_BEFORE_LIMIT_METADATA_NAME, $table->getRowsCount()); if ($this->keepSummaryRow) { $summaryRow = $table->getRowFromId(DataTable::ID_SUMMARY_ROW); } // we delete from 0 to offset if ($this->offset > 0) { $table->deleteRowsOffset(0, $this->offset); } // at this point the array has offset less elements. We delete from limit to the end if ($this->limit >= 0) { $table->deleteRowsOffset($this->limit); } if ($this->keepSummaryRow && !empty($summaryRow)) { $table->addSummaryRow($summaryRow); } }
/** * @param DataTable $table */ public function filter($table) { $idSubtable = $this->idSubtable ?: $table->getId(); $table->queueFilter('ColumnCallbackAddMetadata', array('label', 'url', 'Piwik\\Plugins\\Referrers\\getSearchEngineUrlFromName')); $table->queueFilter('MetadataCallbackAddMetadata', array('url', 'logo', 'Piwik\\Plugins\\Referrers\\getSearchEngineLogoFromUrl')); // get the keyword and create the URL to the search result page $rootRow = $this->firstLevelKeywordTable->getRowFromIdSubDataTable($idSubtable); if ($rootRow) { $keyword = $rootRow->getColumn('label'); $table->queueFilter('MetadataCallbackReplace', array('url', 'Piwik\\Plugins\\Referrers\\getSearchEngineUrlFromUrlAndKeyword', array($keyword))); $table->queueFilter(function (DataTable $table) { $row = $table->getRowFromId(DataTable::ID_SUMMARY_ROW); if ($row) { $row->deleteMetadata('url'); } }); } }
/** * See {@link ReplaceSummaryRowLabel}. * * @param DataTable $table */ public function filter($table) { $row = $table->getRowFromId(DataTable::ID_SUMMARY_ROW); if ($row) { $row->setColumn('label', $this->newLabel); } else { $row = $table->getRowFromLabel(DataTable::LABEL_SUMMARY_ROW); if ($row) { $row->setColumn('label', $this->newLabel); } } // recurse foreach ($table->getRowsWithoutSummaryRow() as $row) { $subTable = $row->getSubtable(); if ($subTable) { $this->filter($subTable); } } }
/** * @param DataTable $table */ public function filter($table) { $idSubtable = $this->idSubtable ?: $table->getId(); $table->queueFilter('ColumnCallbackAddMetadata', array('label', 'url', function ($url) { return SearchEngine::getInstance()->getUrlFromName($url); })); $table->queueFilter('MetadataCallbackAddMetadata', array('url', 'logo', function ($url) { return SearchEngine::getInstance()->getLogoFromUrl($url); })); // get the keyword and create the URL to the search result page $rootRow = $this->firstLevelKeywordTable->getRowFromIdSubDataTable($idSubtable); if ($rootRow) { $keyword = $rootRow->getColumn('label'); $table->queueFilter('MetadataCallbackReplace', array('url', function ($url, $keyword) { return SearchEngine::getInstance()->getBackLinkFromUrlAndKeyword($url, $keyword); }, array($keyword))); $table->queueFilter(function (DataTable $table) { $row = $table->getRowFromId(DataTable::ID_SUMMARY_ROW); if ($row) { $row->deleteMetadata('url'); } }); } }
/** * 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 assertColumnsOfRowIdEquals($expectedColumns, $rowId) { $this->assertSame($expectedColumns, $this->table->getRowFromId($rowId)->getColumns()); }
/** * Helper function to display operation/cluster summary stats. * * @param $idSite * @param $period * @param $date * @param $settings * * @return \Piwik\DataTable */ protected function processSummary($idSite, $period, $date, $settings, $addCountry = true) { $table = new DataTable(); $segment = $settings['name'] . '==' . $settings['context'] . ';' . $settings['value'] . '=@' . $settings['id']; // Build an archive to retrieve the information from the records. $archive = Archive::build($idSite, $period, $date, $segment); /* @var \Piwik\DataTable $tdata */ $data = $archive->getDataTableFromNumeric(array('nb_visits', 'Actions_nb_downloads')); // Add the downloads by type. $downloads = $this->attachDownloadByType($idSite, $period, $date, $segment); $data->getRowFromId(0)->addColumns($downloads); $table->addRow($data->getRowFromId(0)); // Calculate the right label and load the content. if (empty($settings['label'])) { $base_url = 'https://www.humanitarianresponse.info/api/v1.0/'; if ($content_raw = @file_get_contents($base_url . $settings['type'] . '/' . $settings['id'])) { $hrContent = json_decode($content_raw); $settings['label'] = $hrContent->data[0]->label; } } // Add the label to the row. $table->getRowFromId(0)->addColumn('label', $settings['label']); // Only add country stats on demand. if ($addCountry && !empty($hrContent)) { $dataByCountry = $this->attachStatsbyCountry($hrContent, $idSite, $period, $date, $segment); if ($row = $dataByCountry->getRowFromId(0)) { $table->addRow($row); } } return $table; }
/** * @param DataTable $table */ private function addSummaryRow($table) { if ($table->getRowsCount() <= $this->truncateAfter + 1) { return; } $table->filter('Sort', array($this->columnToSortByBeforeTruncating, 'desc', $naturalSort = true, $recursiveSort = false)); $rows = array_values($table->getRows()); $count = $table->getRowsCount(); $newRow = new Row(array(Row::COLUMNS => array('label' => DataTable::LABEL_SUMMARY_ROW))); $aggregationOps = $table->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME); for ($i = $this->truncateAfter; $i < $count; $i++) { if (!isset($rows[$i])) { // case when the last row is a summary row, it is not indexed by $cout but by DataTable::ID_SUMMARY_ROW $summaryRow = $table->getRowFromId(DataTable::ID_SUMMARY_ROW); //FIXME: I'm not sure why it could return false, but it was reported in: http://forum.piwik.org/read.php?2,89324,page=1#msg-89442 if ($summaryRow) { $newRow->sumRow($summaryRow, $enableCopyMetadata = false, $aggregationOps); } } else { $newRow->sumRow($rows[$i], $enableCopyMetadata = false, $aggregationOps); } } $table->filter('Limit', array(0, $this->truncateAfter)); $table->addSummaryRow($newRow); unset($rows); }