You are here

protected function Hosts::_getData in Visitors 8.2

Returns a table content.

Parameters

array $header: Table header configuration.

Return value

array Array representing the table content.

1 call to Hosts::_getData()
Hosts::display in src/Controller/Report/Hosts.php
Returns a hosts page.

File

src/Controller/Report/Hosts.php, line 118
Contains Drupal\visitors\Controller\Report\Hosts.

Class

Hosts

Namespace

Drupal\visitors\Controller\Report

Code

protected function _getData($header) {
  $items_per_page = \Drupal::config('visitors.config')
    ->get('items_per_page');
  $query = \Drupal::database()
    ->select('visitors', 'v')
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
    ->extend('Drupal\\Core\\Database\\Query\\TableSortExtender');
  $query
    ->addExpression('COUNT(*)', 'count');
  $query
    ->fields('v', array(
    'visitors_ip',
  ));
  visitors_date_filter_sql_condition($query);
  $query
    ->groupBy('visitors_ip');
  $query
    ->orderByHeader($header);
  $query
    ->limit($items_per_page);
  $count_query = \Drupal::database()
    ->select('visitors', 'v');
  $count_query
    ->addExpression('COUNT(DISTINCT visitors_ip)');
  visitors_date_filter_sql_condition($count_query);
  $query
    ->setCountQuery($count_query);
  $results = $query
    ->execute();
  $whois_enable = \Drupal::service('module_handler')
    ->moduleExists('whois');
  $attr = array(
    'attributes' => array(
      'target' => '_blank',
      'title' => t('Whois lookup'),
    ),
  );
  $rows = array();
  $page = isset($_GET['page']) ? $_GET['page'] : 0;
  $i = 0 + $page * $items_per_page;
  foreach ($results as $data) {
    $ip = long2ip($data->visitors_ip);
    $visitors_host_url = Url::fromRoute('visitors.host_hits', array(
      "host" => $ip,
    ));
    $visitors_host_link = Link::fromTextAndUrl($ip, $visitors_host_url);
    $visitors_host_link = $visitors_host_link
      ->toRenderable();

    //@TODO 8.3.X check if whois enable
    $rows[] = array(
      ++$i,
      //$whois_enable ? l($ip, 'whois/' . $ip, $attr) : check_plain($ip),
      $ip,
      $data->count,
      render($visitors_host_link),
    );
  }
  return $rows;
}