You are here

function statistics_counter_cron in Statistics Counter 8

Same name and namespace in other branches
  1. 7 statistics_counter.module \statistics_counter_cron()

Implements hook_cron().

File

./statistics_counter.module, line 11
Statistics Counter

Code

function statistics_counter_cron() {
  $config = \Drupal::configFactory()
    ->getEditable('statistics_counter.settings');

  // Get timestamps.
  $timestamp = $config
    ->get('timestamp', 0);

  // Get date.
  $week = date('W') | 0;
  $month = date('n') | 0;
  $year = date('Y') | 0;
  $last_week = date('W', $timestamp) | 0;
  $last_month = date('n', $timestamp) | 0;
  $last_year = date('Y', $timestamp) | 0;
  $fields = array();
  if ($week != $last_week || $year != $last_year) {

    // Reset week counts.
    $fields['weekcount'] = 0;
  }
  if ($month != $last_month || $year != $last_year) {

    // Reset month counts.
    $fields['monthcount'] = 0;
  }
  if ($year != $last_year) {

    // Reset year counts.
    $fields['yearcount'] = 0;
  }
  if (!empty($fields)) {

    // Reset year counts.
    db_update('node_counter')
      ->fields($fields)
      ->execute();
  }
  $config
    ->set('timestamp', REQUEST_TIME)
    ->save();
}