addRowFromArray() public method

You can add row metadata with this method.
public addRowFromArray ( array $row )
$row array eg. `array(Row::COLUMNS => array('visits' => 13, 'test' => 'toto'), Row::METADATA => array('mymetadata' => 'myvalue'))`
Example #1
0
 /**
  * API method that returns number of visits based on hour parity.
  * @param int    $idSite
  * @param string $period
  * @param string $date
  * @param bool|string $segment
  * @return DataTable
  */
 public function getEvenUnevenTimes($idSite, $period, $date, $segment = false)
 {
     $archive = Archive::build($idSite, $period, $date);
     $oddHoursCount = $archive->getNumeric(Archiver::ODD_HOURS_COUNT_RECORD_NAME);
     $evenHoursCount = $archive->getNumeric(Archiver::EVEN_HOURS_COUNT_RECORD_NAME);
     $table = new DataTable();
     $table->setTableSortedBy('times');
     $table->addRowFromArray(array(Row::COLUMNS => array('times' => Piwik::translate('HourParity_Oddhours'), 'nb_visits' => $oddHoursCount)));
     $table->addRowFromArray(array(Row::COLUMNS => array('times' => Piwik::translate('HourParity_Evenhours'), 'nb_visits' => $evenHoursCount)));
     return $table;
 }
Example #2
0
 /**
  *  test with a row without child
  *
  * @group Core
  */
 public function testConsoleSimple()
 {
     $table = new DataTable();
     $table->addRowFromArray(array(Row::COLUMNS => array('visits' => 245, 'visitors' => 245), Row::METADATA => array('logo' => 'test.png')));
     $expected = "- 1 ['visits' => 245, 'visitors' => 245] ['logo' => 'test.png'] [idsubtable = ]<br />\n";
     $render = new Console();
     $render->setTable($table);
     $rendered = $render->render();
     $this->assertEquals($expected, $rendered);
 }
Example #3
0
 /**
  * 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 getExampleReport($idSite, $period, $date, $segment = false)
 {
     $table = new DataTable();
     $table->addRowFromArray(array(Row::COLUMNS => array('nb_visits' => 5)));
     return $table;
 }
 private function createDataTable($rows)
 {
     $useless1 = new DataTable();
     foreach ($rows as $row) {
         $useless1->addRowFromArray(array(Row::COLUMNS => $row));
     }
     return $useless1;
 }
Example #5
0
 private function addRow(DataTable $table, DataTable\Row $row, $growthPercentage, $newValue, $oldValue, $difference, $disappeared = false, $isNew = false, $isMover = false)
 {
     $columns = $row->getColumns();
     $columns['growth_percent'] = $growthPercentage;
     $columns['growth_percent_numeric'] = str_replace('%', '', $growthPercentage);
     $columns['grown'] = '-' != substr($growthPercentage, 0, 1);
     $columns['value_old'] = $oldValue;
     $columns['value_new'] = $newValue;
     $columns['difference'] = $difference;
     $columns['importance'] = abs($difference);
     $columns['isDisappeared'] = $disappeared;
     $columns['isNew'] = $isNew;
     $columns['isMover'] = $isMover;
     $table->addRowFromArray(array(DataTable\Row::COLUMNS => $columns));
 }
Example #6
0
 /**
  * General tests that tries to test the normal behaviour of DataTable
  *
  * We create some tables, add rows, some of the rows link to sub tables
  *
  * Then we serialize everything, and we check that the unserialize give the same object back
  *
  * @group Core
  */
 public function testGeneral()
 {
     /*
      * create some fake tables to make sure that the serialized array of the first TABLE
      * does not take in consideration those tables
      */
     $useless1 = new DataTable();
     $useless1->addRowFromArray(array(Row::COLUMNS => array(13)));
     /*
      * end fake tables
      */
     /*
      * MAIN TABLE
      */
     $table = new DataTable();
     $subtable = new DataTable();
     $idtable = $table->getId();
     $idsubtable = $subtable->getId();
     /*
      * create some fake tables to make sure that the serialized array of the first TABLE
      * does not take in consideration those tables
      * -> we check that the DataTable_Manager is not impacting DataTable
      */
     $useless1->addRowFromArray(array(Row::COLUMNS => array(8487)));
     $useless3 = new DataTable();
     $useless3->addRowFromArray(array(Row::COLUMNS => array(8487)));
     /*
      * end fake tables
      */
     $row = array(Row::COLUMNS => array(0 => 1554, 1 => 42, 2 => 657, 3 => 155744), Row::METADATA => array('logo' => 'test.png'));
     $row = new Row($row);
     $table->addRow($row);
     $table->addRowFromArray(array(Row::COLUMNS => array(0 => 1554, 1 => 42), Row::METADATA => array('url' => 'piwik.org')));
     $table->addRowFromArray(array(Row::COLUMNS => array(0 => 787877888787), Row::METADATA => array('url' => 'OUPLA ADDED'), Row::DATATABLE_ASSOCIATED => $subtable));
     /*
      * SUB TABLE
      */
     $row = array(Row::COLUMNS => array(0 => 1554), Row::METADATA => array('searchengine' => 'google'));
     $subtable->addRowFromArray($row);
     $row = array(Row::COLUMNS => array(0 => 84894), Row::METADATA => array('searchengine' => 'yahoo'));
     $subtable->addRowFromArray($row);
     $row = array(Row::COLUMNS => array(0 => 4898978989), Row::METADATA => array('searchengine' => 'ask'));
     $subtable->addRowFromArray($row);
     /*
      * SUB SUB TABLE
      */
     $subsubtable = new DataTable();
     $subsubtable->addRowFromArray(array(Row::COLUMNS => array(245), Row::METADATA => array('yes' => 'subsubmetadata1')));
     $subsubtable->addRowFromArray(array(Row::COLUMNS => array(13), Row::METADATA => array('yes' => 'subsubmetadata2')));
     $row = array(Row::COLUMNS => array(0 => 666666666666666), Row::METADATA => array('url' => 'NEW ROW ADDED'), Row::DATATABLE_ASSOCIATED => $subsubtable);
     $subtable->addRowFromArray($row);
     $idsubsubtable = $subsubtable->getId();
     $serialized = $table->getSerialized();
     $this->assertEquals(array_keys($serialized), array($idsubsubtable, $idsubtable, 0));
     // In the next test we compare an unserialized datatable with its original instance.
     // The unserialized datatable rows will have positive DATATABLE_ASSOCIATED ids.
     // Positive DATATABLE_ASSOCIATED ids mean that the associated sub-datatables are not loaded in memory.
     // In this case, this is NOT true: we know that the sub-datatable is loaded in memory.
     // HOWEVER, because of datatable id conflicts happening in the datatable manager, it is not yet
     // possible to know, after unserializing a datatable, if its sub-datatables are loaded in memory.
     $expectedTableRows = array();
     foreach ($table->getRows() as $currentRow) {
         $expectedTableRow = clone $currentRow;
         $currentRowAssociatedDatatableId = $currentRow->c[Row::DATATABLE_ASSOCIATED];
         if ($currentRowAssociatedDatatableId != null) {
             // making DATATABLE_ASSOCIATED ids positive
             $expectedTableRow->c[Row::DATATABLE_ASSOCIATED] = -1 * $currentRowAssociatedDatatableId;
         }
         $expectedTableRows[] = $expectedTableRow;
     }
     $tableAfter = new DataTable();
     $tableAfter->addRowsFromSerializedArray($serialized[0]);
     $this->assertEquals($expectedTableRows, $tableAfter->getRows());
     $subsubtableAfter = new DataTable();
     $subsubtableAfter->addRowsFromSerializedArray($serialized[$idsubsubtable]);
     $this->assertEquals($subsubtable->getRows(), $subsubtableAfter->getRows());
     $this->assertEquals($subsubtable->getRows(), DataTable::fromSerializedArray($serialized[$idsubsubtable])->getRows());
     $this->assertTrue($subsubtable->getRowsCount() > 0);
     $this->assertEquals($table, Manager::getInstance()->getTable($idtable));
     $this->assertEquals($subsubtable, Manager::getInstance()->getTable($idsubsubtable));
 }
 public function getVisitorEmailFromJson($json_data)
 {
     \Piwik\Piwik::checkUserHasSomeViewAccess();
     $table = new DataTable();
     $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode($json_data)));
     foreach ($iterator as $key => $value) {
         if (filter_var($value, FILTER_VALIDATE_EMAIL) && !in_array($value, $result)) {
             $result[] = $value;
         }
     }
     foreach ($result as $email) {
         $table->addRowFromArray(array(Row::COLUMNS => array('email' => $email, 'idvisitor' => $idvisitor)));
     }
     return $table;
 }
Example #8
0
 /**
  * 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 getFrontEndPerformance($idSite, $period, $date, $segment = false)
 {
     $table = new DataTable();
     $table->addRowFromArray(array(Row::COLUMNS => array('nb_visits' => 5)));
     return $table;
 }