You are here

static function StatisticsCounterSubscriber::updateStatistics in Statistics Counter 8

Update statistics.

Parameters

Symfony\Component\EventDispatcher\Event $event: Event.

File

src/EventSubscriber/StatisticsCounterSubscriber.php, line 28

Class

StatisticsCounterSubscriber
Subscribe to KernelEvents::TERMINATE events to recalculate nodes statistics.

Namespace

Drupal\statistics_counter\EventSubscriber

Code

static function updateStatistics(Event $event) {
  $node = \Drupal::request()->attributes
    ->get('node');
  $views = \Drupal::config('statistics.settings')
    ->get('count_content_views');
  if ($node && $event
    ->getResponse() instanceof HtmlResponse && $views) {

    // Support statistics filter.
    if (\Drupal::moduleHandler()
      ->moduleExists('statistics_filter') && statistics_filter_do_filter()) {
      return;
    }

    // We are counting content views.
    // A node has been viewed, so update the node's counters.
    db_merge('node_counter')
      ->key(array(
      'nid' => $node
        ->id(),
    ))
      ->fields(array(
      'weekcount' => 1,
      'monthcount' => 1,
      'yearcount' => 1,
    ))
      ->expression('weekcount', 'weekcount + 1')
      ->expression('monthcount', 'monthcount + 1')
      ->expression('yearcount', 'yearcount + 1')
      ->execute();
  }
}