/** * Filters a row's subtable, if one exists and is loaded in memory. * * @param Row $row The row whose subtable should be filter. */ public function filterSubTable(Row $row) { if (!$this->enableRecursive) { return; } if ($row->isSubtableLoaded()) { $subTable = Manager::getInstance()->getTable($row->getIdSubDataTable()); $this->filter($subTable); } }
private function loadSubtable(DataTable $table, Row $row) { $idSubtable = $row->getIdSubDataTable(); if ($idSubtable === null) { return null; } $subtable = $row->getSubtable(); if (!$subtable) { $subtable = $this->thisReport->fetchSubtable($idSubtable, $this->getRequestParamOverride($table)); } if (!$subtable) { // sanity check throw new Exception("Unexpected error: could not load subtable '{$idSubtable}'."); } return $subtable; }
/** * Load the subtable for a row. * Returns null if none is found. * * @param DataTable $dataTable * @param Row $row * * @return DataTable */ protected function loadSubtable($dataTable, $row) { if (!($this->apiModule && $this->apiMethod && count($this->request))) { return null; } $request = $this->request; $idSubTable = $row->getIdSubDataTable(); if ($idSubTable === null) { return null; } $request['idSubtable'] = $idSubTable; if ($dataTable) { $period = $dataTable->getMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX); if ($period instanceof Range) { $request['date'] = $period->getDateStart() . ',' . $period->getDateEnd(); } else { $request['date'] = $period->getDateStart()->toString(); } } $method = $this->getApiMethodForSubtable(); return $this->callApiAndReturnDataTable($this->apiModule, $method, $request); }
/** * Simple test of the DataTable_Row */ public function testRow() { $columns = array('test_column' => 145, 92582495 => new Timer(), 'super' => array('this column has an array value, amazing')); $metadata = array('logo' => 'piwik.png', 'super' => array('this column has an array value, amazing')); $arrayRow = array(Row::COLUMNS => $columns, Row::METADATA => $metadata, 'fake useless key' => 38959, 43905724897 => 'value'); $row = new Row($arrayRow); $this->assertEquals($columns, $row->getColumns()); $this->assertEquals($metadata, $row->getMetadata()); $this->assertNull($row->getIdSubDataTable()); }
/** * Helper function that tests if two rows are equal. * * Two rows are equal if: * * - they have exactly the same columns / metadata * - they have a subDataTable associated, then we check that both of them are the same. * * Column order is not important. * * @param \Piwik\DataTable\Row $row1 first to compare * @param \Piwik\DataTable\Row $row2 second to compare * @return bool */ public static function isEqual(Row $row1, Row $row2) { //same columns $cols1 = $row1->getColumns(); $cols2 = $row2->getColumns(); $diff1 = array_udiff($cols1, $cols2, array(__CLASS__, 'compareElements')); $diff2 = array_udiff($cols2, $cols1, array(__CLASS__, 'compareElements')); if ($diff1 != $diff2) { return false; } $dets1 = $row1->getMetadata(); $dets2 = $row2->getMetadata(); ksort($dets1); ksort($dets2); if ($dets1 != $dets2) { return false; } // either both are null // or both have a value if (!(is_null($row1->getIdSubDataTable()) && is_null($row2->getIdSubDataTable()))) { $subtable1 = $row1->getSubtable(); $subtable2 = $row2->getSubtable(); if (!DataTable::isEqual($subtable1, $subtable2)) { return false; } } return true; }
public function test_isSubtableLoaded_ShouldReturnFalse_WhenRestoringAnExportedRow() { $testRow = $this->getTestRowWithSubDataTableLoaded(); // serialize and unserialize is not needed for this test case, the export is the important part. // we still do it, to have it more "realistic" $serializedTestRow = serialize($testRow->export()); $unserializedTestRow = unserialize($serializedTestRow); /** @var Row $unserializedTestRow */ $row = new Row($unserializedTestRow); $this->assertTrue($row->getIdSubDataTable() > 0); $this->assertFalse($row->isSubtableLoaded()); }