You are here

protected function Cities::_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 Cities::_getData()
Cities::display in src/Controller/Report/Cities.php
Returns a cities page.

File

src/Controller/Report/Cities.php, line 115
Contains Drupal\visitors\Controller\Report\Cities.

Class

Cities

Namespace

Drupal\visitors\Controller\Report

Code

protected function _getData($header, $country) {
  $items_per_page = \Drupal::config('visitors.config')
    ->get('items_per_page');
  $original_country = $country == '(none)' ? '' : $country;
  $query = \Drupal::database()
    ->select('visitors', 'v')
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
    ->extend('Drupal\\Core\\Database\\Query\\TableSortExtender');
  $query
    ->addExpression('COUNT(visitors_city)', 'count');
  $query
    ->fields('v', array(
    'visitors_city',
  ));
  $query
    ->condition('v.visitors_country_name', $original_country);
  visitors_date_filter_sql_condition($query);
  $query
    ->groupBy('visitors_city');
  $query
    ->orderByHeader($header);
  $query
    ->limit($items_per_page);
  $count_query = \Drupal::database()
    ->select('visitors', 'v');
  $count_query
    ->addExpression('COUNT(DISTINCT visitors_city)');
  $count_query
    ->condition('v.visitors_country_name', $original_country);
  visitors_date_filter_sql_condition($count_query);
  $query
    ->setCountQuery($count_query);
  $results = $query
    ->execute();
  $rows = array();
  $page = isset($_GET['page']) ? $_GET['page'] : 0;
  $i = 0 + $page * $items_per_page;
  foreach ($results as $data) {
    if ($data->visitors_city == '') {
      $data->visitors_city = '(none)';
    }
    $visitors_city_url = Url::fromRoute('visitors.city_hits', array(
      "country" => $country,
      "city" => $data->visitors_city,
    ));
    $visitors_city_link = Link::fromTextAndUrl($this
      ->t($data->visitors_city), $visitors_city_url);
    $visitors_city_link = $visitors_city_link
      ->toRenderable();
    $rows[] = array(
      ++$i,
      render($visitors_city_link),
      $data->visitors_city,
      $data->count,
    );
  }
  return $rows;
}