addRowFromSimpleArray() public method

Row metadata cannot be added with this method.
public addRowFromSimpleArray ( array $row )
$row array eg. `array('name' => 'google analytics', 'license' => 'commercial')`
コード例 #1
0
 public function test_renderDataTable_shouldReturnResult()
 {
     $dataTable = new DataTable();
     $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
     $response = $this->builder->renderDataTable($dataTable);
     $this->assertSame("- 1 ['nb_visits' => 5, 'nb_random' => 10] [] [idsubtable = ]<br />\n", $response);
 }
コード例 #2
0
 public function test_renderDataTable_shouldSerializeIfEnabled()
 {
     $dataTable = new DataTable();
     $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
     $builder = $this->makeBuilder(array('serialize' => 1));
     $response = $builder->renderDataTable($dataTable);
     $this->assertSame('O:15:"Piwik\\DataTable":2:{s:7:"*rows";a:1:{i:0;O:19:"Piwik\\DataTable\\Row":1:{s:1:"c";a:3:{i:0;a:2:{s:9:"nb_visits";i:5;s:9:"nb_random";i:10;}i:1;a:0:{}i:3;N;}}}s:13:"*summaryRow";N;}', $response);
 }
コード例 #3
0
 public function test_renderDataTable_shouldSerializeIfEnabled()
 {
     $dataTable = new DataTable();
     $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
     $builder = $this->makeBuilder(array('serialize' => 1));
     $response = $builder->renderDataTable($dataTable);
     $builder = $this->makeBuilder(array('serialize' => 0));
     $original = $builder->renderDataTable($dataTable);
     $expected = serialize($original);
     $this->assertEquals($expected, $response);
 }
コード例 #4
0
    public function test_getResponse_shouldHandleDataTable()
    {
        $builder = new ResponseBuilder('xml', array());
        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
        $response = $builder->getResponse($dataTable);
        $this->assertEquals('<?xml version="1.0" encoding="utf-8" ?>
<result>
	<row>
		<nb_visits>5</nb_visits>
		<nb_random>10</nb_random>
	</row>
</result>', $response);
    }
コード例 #5
0
ファイル: API.php プロジェクト: carriercomm/piwik
 /**
  * @param DataTable $table
  * @param int $idSite
  * @param string $period
  * @param string $date
  * @return mixed
  */
 protected function removeHoursInFuture($table, $idSite, $period, $date)
 {
     $site = new Site($idSite);
     if ($period == 'day' && ($date == 'today' || $date == Date::factory('now', $site->getTimezone())->toString())) {
         $currentHour = Date::factory('now', $site->getTimezone())->toString('G');
         // If no data for today, this is an exception to the API output rule, as we normally return nothing:
         // we shall return all hours of the day, with nb_visits = 0
         if ($table->getRowsCount() == 0) {
             for ($hour = 0; $hour <= $currentHour; $hour++) {
                 $table->addRowFromSimpleArray(array('label' => $hour, 'nb_visits' => 0));
             }
             return $table;
         }
         $idsToDelete = array();
         foreach ($table->getRows() as $id => $row) {
             $hour = $row->getColumn('label');
             if ($hour > $currentHour) {
                 $idsToDelete[] = $id;
             }
         }
         $table->deleteRows($idsToDelete);
     }
     return $table;
 }
コード例 #6
0
ファイル: ApiTest.php プロジェクト: JoeHorn/piwik
 public function test_generateReport_CatchesIndividualReportProcessExceptions_WithoutFailingToGenerateWholeReport()
 {
     $realProxy = new Proxy();
     $mockProxy = $this->getMock('Piwik\\API\\Proxy', array('call'));
     $mockProxy->expects($this->any())->method('call')->willReturnCallback(function ($className, $methodName, $parametersRequest) use($realProxy) {
         switch ($className) {
             case '\\Piwik\\Plugins\\VisitsSummary\\API':
                 $result = new DataTable();
                 $result->addRowFromSimpleArray(array('label' => 'visits label', 'nb_visits' => 1));
                 return $result;
             case '\\Piwik\\Plugins\\UserCountry\\API':
                 throw new \Exception("error");
             case '\\Piwik\\Plugins\\Referrers\\API':
                 $result = new DataTable();
                 $result->addRowFromSimpleArray(array('label' => 'referrers label', 'nb_visits' => 1));
                 return $result;
             case '\\Piwik\\Plugins\\API\\API':
                 return $realProxy->call($className, $methodName, $parametersRequest);
             default:
                 throw new \Exception("Unexpected method {$className}::{$methodName}.");
         }
     });
     Proxy::setSingletonInstance($mockProxy);
     $idReport = APIScheduledReports::getInstance()->addReport(1, '', Schedule::PERIOD_DAY, 0, ScheduledReports::EMAIL_TYPE, ReportRenderer::HTML_FORMAT, array('VisitsSummary_get', 'UserCountry_getCountry', 'Referrers_getWebsites'), array(ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_TABLES_ONLY));
     ob_start();
     $result = APIScheduledReports::getInstance()->generateReport($idReport, Date::factory('now')->toString(), $language = false, $outputType = APIScheduledReports::OUTPUT_RETURN);
     ob_end_clean();
     $this->assertContains('id="VisitsSummary_get"', $result);
     $this->assertContains('id="Referrers_getWebsites"', $result);
     $this->assertNotContains('id="UserCountry_getCountry"', $result);
 }
コード例 #7
0
ファイル: PhpRendererTest.php プロジェクト: Abine/piwik
 public function test_renderDataTable_shouldRenderDataTableMaps()
 {
     $map = new DataTable\Map();
     $dataTable = new DataTable();
     $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
     $dataTable2 = new DataTable();
     $dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
     $map->addTable($dataTable, 'table1');
     $map->addTable($dataTable2, 'table2');
     $response = $this->builder->renderDataTable($map);
     $expected = array('table1' => array(array('nb_visits' => 5, 'nb_random' => 10)), 'table2' => array(array('nb_visits' => 3, 'nb_random' => 6)));
     $this->assertEquals($expected, $response);
 }
コード例 #8
0
ファイル: API.php プロジェクト: KiwiJuicer/handball-dachau
 /**
  * Returns a custom data table.
  * This data table will be converted to all available formats
  * when requested in the API request.
  *
  * @return DataTable
  */
 public function getCompetitionDatatable()
 {
     $dataTable = new DataTable();
     $row1 = new Row();
     $row1->setColumns(array('name' => 'piwik', 'license' => 'GPL'));
     // Rows Metadata is useful to store non stats data for example (logos, urls, etc.)
     // When printed out, they are simply merged with columns
     $row1->setMetadata('logo', 'logo.png');
     $dataTable->addRow($row1);
     $dataTable->addRowFromSimpleArray(array('name' => 'google analytics', 'license' => 'commercial'));
     return $dataTable;
 }
コード例 #9
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 getData($idSite, $period, $date, $segment = false)
    {
        // Not: \Piwik is \core
        // TODO: Zugriffssteuerung !!!
        \Piwik\Piwik::checkUserHasViewAccess($idSite);
        // Read additional variables
        $aLimit = (int) \Piwik\Common::getRequestVar('vlimit', 100, 'int');
        if ($aLimit === 0) {
            $aLimit = 100;
        }
        $getVar = trim((string) \Piwik\Common::getRequestVar('casevar', '', 'string'));
        $clipServer = (string) \Piwik\Common::getRequestVar('server', 'no', 'string') !== 'yes';
        $clipExtension = (string) \Piwik\Common::getRequestVar('noclip', 'no', 'string') !== 'yes';
        $recodeIndex = (string) \Piwik\Common::getRequestVar('disindex', 'no', 'string') !== 'yes';
        $skipNoID = (string) \Piwik\Common::getRequestVar('skipid', 'no', 'string') === 'yes';
        $addAggregate = (string) \Piwik\Common::getRequestVar('aggregate', 'no', 'string') === 'yes';
        $structure = (string) \Piwik\Common::getRequestVar('structure', 'case', 'string');
        // [case|page]
        // https://developer.piwik.org/guides/security-in-piwik
        // https://developer.piwik.org/guides/persistence-and-the-mysql-backend
        $sql = 'SELECT v.idvisit, l.server_time, l.time_spent_ref_action, a.name, a.idaction
			FROM ' . \Piwik\Common::prefixTable('log_visit') . ' v
			LEFT JOIN ' . \Piwik\Common::prefixTable('log_link_visit_action') . ' l ON (v.idvisit = l.idvisit)
			LEFT JOIN ' . \Piwik\Common::prefixTable('log_action') . ' a ON (a.idaction = l.idaction_url)
			WHERE (v.idsite = ?) AND (a.type = 1)
			ORDER BY l.idvisit, l.server_time';
        $res = \Piwik\Db::query($sql, array($idSite));
        // error_log(var_export($idSite, true));
        if (empty($getVar)) {
            $getVarLen = 0;
            $skipNoID = false;
        } else {
            $getVarLen = strlen($getVar) + 1;
        }
        $nAction = 0;
        $nActionMax = 0;
        // Number of actions per visit (max.)
        $cvVisitID = null;
        $cvCaseID = null;
        $cvCaseStart = null;
        $cvTimeSum = 0;
        $cvCaseActions = array();
        $cvCaseTimes = array();
        $cvCaseStamps = array();
        $visits = array();
        $actionIDs = array();
        // Collect IDs per action
        do {
            $row = $res->fetch();
            // Stop processing for empty results
            if ($row === false) {
                if ($cvVisitID === null) {
                    // No data at all
                    $table->addRowFromSimpleArray(array('error' => 'no data available', 'site' => $idSite));
                    return $table;
                }
            }
            $cvFirst = false;
            // Store new visit
            if ($row === false or $row['idvisit'] !== $cvVisitID and $cvVisitID !== null) {
                if ($skipNoID and $cvCaseID === null) {
                    // Skip this case
                } else {
                    $visits[] = array('id' => $cvVisitID, 'case' => $cvCaseID, 'start' => $cvCaseStart, 'actions' => $cvCaseActions, 'times' => $cvCaseTimes, 'stamps' => $cvCaseStamps, 'time sum' => $cvTimeSum);
                    if (count($cvCaseActions) > $nAction) {
                        $nAction = count($cvCaseActions);
                    }
                    if ($nAction > $nActionMax) {
                        $nActionMax = $nAction;
                    }
                }
            }
            // Stop, if there is no more data
            if ($row === false) {
                break;
            }
            // Register new visit (group of actions/page views)
            if ($row['idvisit'] !== $cvVisitID) {
                $nAction = 1;
                $cvVisitID = $row['idvisit'];
                $cvCaseStart = $row['server_time'];
                $cvTimeSum = 0;
                $cvCaseID = null;
                $cvCaseActions = array();
                $cvCaseTimes = array();
                $cvCaseStamps = array();
            } else {
                $nAction++;
            }
            // Decode the URL
            $url = $row['name'];
            if ($cvCaseID === null and $getVarLen !== 0) {
                if (($p = strpos($url, '?')) !== false) {
                    $qs = substr($url, $p + 1);
                    $items = explode('&', $qs);
                    foreach ($items as $item) {
                        if (substr($item, 0, $getVarLen) === $getVar . '=') {
                            $s = trim(substr($item, $getVarLen));
                            if ($s !== '') {
                                $cvCaseID = $s;
                            }
                        }
                    }
                }
            }
            if ($recodeIndex) {
                $t10 = substr($url, -10);
                $t11 = substr($url, -11);
                if ($t10 === '/index.php' or $t10 === '/index.htm' or $t11 === '/index.html') {
                    // No index in subdirectories
                    if (substr_count($url, '/') === 1) {
                        $p = strpos($url, '/');
                        $url = substr($url, 0, $p + 1);
                    }
                }
            }
            if ($clipServer) {
                $p = strpos($url, '/');
                if ($p !== false) {
                    $url = substr($url, $p + 1);
                    if ($url === false) {
                        $url = '';
                    }
                }
            }
            if ($clipExtension) {
                $p1 = strrpos($url, '.');
                $p2 = strrpos($url, '?');
                if ($p1 === false and $p2 === false) {
                    // No extension
                    $p = false;
                } elseif ($p1 === false) {
                    $p = $p2;
                } elseif ($p2 === false) {
                    $p = $p1;
                } else {
                    $p = min($p1, $p2);
                }
                if ($p !== false) {
                    $url = substr($url, 0, $p);
                }
            }
            if ($url === '') {
                $url = '/';
            }
            $cvCaseActions[] = $url;
            $aTime = (int) $row['time_spent_ref_action'];
            $cvCaseTimes[] = $aTime;
            $cvCaseStamps[] = $row['server_time'];
            $cvTimeSum += $aTime;
            // Already know this action?
            if (!isset($actionIDs[$url])) {
                $actionIDs[$url] = (int) $row['idaction'];
            }
        } while ($row !== false);
        // Result table
        $table = new DataTable();
        if ($structure === 'page') {
            foreach ($visits as $visit) {
                $basic = array('id' => $visit['id'], 'CASE' => $visit['case']);
                $t0 = strtotime($visit['start']);
                // One row per action
                $aData = array();
                foreach ($visit['actions'] as $i => $action) {
                    $aData['pos'] = $i + 1;
                    $aData['aID'] = isset($actionIDs[$action]) ? $actionIDs[$action] : -1;
                    $aData['url'] = $action;
                    if (isset($visit['times'][$i + 1])) {
                        $aData['time'] = $visit['times'][$i + 1];
                    } else {
                        $aData['time'] = null;
                    }
                    $aData['ontime'] = strtotime($visit['stamps'][$i]) - $t0;
                    $aData['astime'] = $visit['stamps'][$i];
                    $table->addRowFromSimpleArray(array_merge($basic, $aData));
                }
            }
        } else {
            // Number of actions
            if ($nActionMax > $aLimit) {
                $nActionMax = $aLimit;
            }
            // Collect all possible activities
            if ($addAggregate) {
                $hash = array();
                foreach ($visits as $visit) {
                    foreach ($visit['actions'] as $name) {
                        $hash[$name] = true;
                    }
                }
                $allActivities = array_keys($hash);
                sort($allActivities);
                // Create table for variable names
                $aggVarNames = array();
                foreach ($allActivities as $url) {
                    $aggVarNames[$url] = 'AT_' . preg_replace('/[^a-z0-9]+/i', '_', $url);
                }
            }
            // Sort and package into table
            foreach ($visits as $visit) {
                $basic = array('id' => $visit['id'], 'CASE' => $visit['case'], 'T0' => $visit['start']);
                // Rearrange data, if necessary
                $actions = array();
                $timesT = array();
                $timesAT = array();
                $pages = array();
                for ($i = 0; $i < $nActionMax; $i++) {
                    if (isset($visit['actions'][$i])) {
                        $actions['A' . ($i + 1)] = $visit['actions'][$i];
                    } else {
                        $actions['A' . ($i + 1)] = null;
                    }
                    if (isset($visit['times'][$i + 1])) {
                        $timesT['T' . ($i + 1)] = $visit['times'][$i + 1];
                    } else {
                        $timesT['T' . ($i + 1)] = null;
                    }
                }
                if ($addAggregate) {
                    foreach ($allActivities as $url) {
                        $timesAT[$aggVarNames[$url]] = null;
                    }
                    foreach ($visit['actions'] as $i => $url) {
                        if ($timesAT[$aggVarNames[$url]] === null) {
                            $timesAT[$aggVarNames[$url]] = isset($visit['times'][$i + 1]) ? $visit['times'][$i + 1] : 0;
                        } elseif (isset($visit['times'][$i + 1])) {
                            $timesAT[$aggVarNames[$url]] += $visit['times'][$i + 1];
                        }
                    }
                }
                $table->addRowFromSimpleArray(array_merge($basic, $actions, $timesT, array('AT' => $visit['time sum']), $timesAT, $pages));
            }
        }
        return $table;
    }
コード例 #10
0
ファイル: CsvRendererTest.php プロジェクト: diosmosis/piwik
    public function test_renderDataTable_shouldRenderDataTableMaps()
    {
        $map = new DataTable\Map();
        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
        $dataTable2 = new DataTable();
        $dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
        $map->addTable($dataTable, 'table1');
        $map->addTable($dataTable2, 'table2');
        $response = $this->builder->renderDataTable($map);
        $this->assertEquals('defaultKeyName,nb_visits,nb_random
table1,5,10
table2,3,6', $response);
    }
コード例 #11
0
ファイル: API.php プロジェクト: carriercomm/piwik
 /**
  * Returns a datatable representation of a set of table statuses.
  *
  * @param array $statuses The table statuses to summarize.
  * @return DataTable
  */
 private function getTablesSummary($statuses)
 {
     $dataTable = new DataTable();
     foreach ($statuses as $status) {
         $dataTable->addRowFromSimpleArray(array('label' => $status['Name'], 'data_size' => $status['Data_length'], 'index_size' => $status['Index_length'], 'row_count' => $status['Rows']));
     }
     return $dataTable;
 }
コード例 #12
0
    public function test_renderDataTable_shouldRenderDataTableMaps()
    {
        $map = new DataTable\Map();
        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
        $dataTable2 = new DataTable();
        $dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
        $map->addTable($dataTable, 'table1');
        $map->addTable($dataTable2, 'table2');
        $response = $this->builder->renderDataTable($map);
        $this->assertEquals('<table id="MultiSites_getAll" border="1">
<thead>
	<tr>
		<th>_defaultKeyName</th>
		<th>nb_visits</th>
		<th>nb_random</th>
	</tr>
</thead>
<tbody>
	<tr>
		<td>table1</td>
		<td>5</td>
		<td>10</td>
	</tr>
	<tr>
		<td>table2</td>
		<td>3</td>
		<td>6</td>
	</tr>
</tbody>
</table>
', $response);
    }
コード例 #13
0
ファイル: UserIdTest.php プロジェクト: cemo/piwik
 private function getDataTableWithUsers()
 {
     $tableWithUsers = new DataTable();
     $tableWithUsers->addRowFromSimpleArray(array('label' => 'test', 'nb_users' => 10));
     return $tableWithUsers;
 }
コード例 #14
0
ファイル: XmlRendererTest.php プロジェクト: a4tunado/piwik
    public function test_renderDataTable_shouldRenderDataTableMaps()
    {
        $map = new DataTable\Map();
        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
        $dataTable2 = new DataTable();
        $dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
        $map->addTable($dataTable, 'table1');
        $map->addTable($dataTable2, 'table2');
        $response = $this->builder->renderDataTable($map);
        $this->assertEquals('<?xml version="1.0" encoding="utf-8" ?>
<results>
	<result defaultKeyName="table1">
		<row>
			<nb_visits>5</nb_visits>
			<nb_random>10</nb_random>
		</row>
	</result>
	<result defaultKeyName="table2">
		<row>
			<nb_visits>3</nb_visits>
			<nb_random>6</nb_random>
		</row>
	</result>
</results>', $response);
    }
コード例 #15
0
ファイル: API.php プロジェクト: KiwiJuicer/handball-dachau
 /**
  * @param DataTable|DataTable\Map $dataTable
  * @param $fieldsToGet
  * @param $sitesToProblablyAdd
  */
 private function addMissingWebsites($dataTable, $fieldsToGet, $sitesToProblablyAdd)
 {
     $siteIdsInDataTable = array();
     foreach ($dataTable->getRows() as $row) {
         /** @var DataTable\Row $row */
         $siteIdsInDataTable[] = $row->getColumn('label');
     }
     foreach ($sitesToProblablyAdd as $site) {
         if (!in_array($site['idsite'], $siteIdsInDataTable)) {
             $siteRow = array_combine($fieldsToGet, array_pad(array(), count($fieldsToGet), 0));
             $siteRow['label'] = (int) $site['idsite'];
             $dataTable->addRowFromSimpleArray($siteRow);
         }
     }
 }
コード例 #16
0
 /**
  * @expectedException \Exception
  * @expectedExceptionMessage RSS feeds can be generated for one specific website
  */
 public function test_renderDataTable_shouldFail_IfKeynameIsNotDate()
 {
     $map = new DataTable\Map();
     $dataTable = new DataTable();
     $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
     $map->addTable($dataTable, 'table1');
     $map->addTable($dataTable, 'table2');
     $this->builder->renderDataTable($map);
 }
コード例 #17
0
ファイル: JsonRendererTest.php プロジェクト: a4tunado/piwik
 public function test_renderDataTable_shouldWrapADataTable()
 {
     $builder = $this->makeBuilder(array('callback' => 'myName'));
     $dataTable = new DataTable();
     $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
     $response = $builder->renderDataTable($dataTable);
     $this->assertEquals('myName([{"nb_visits":5,"nb_random":10}])', $response);
     $this->assertNoJsonError($response);
 }