Commit c539835c authored by Gaurav Kapoor's avatar Gaurav Kapoor Committed by Gaurav Kapoor
Browse files

Issue #3264628 by gaurav.kapoor: Add a counter service with all the utility methods

parent 1b67ae53
Loading
Loading
Loading
Loading
+72 −3
Original line number Diff line number Diff line
@@ -56,11 +56,11 @@ class CounterUtility {
  /**
   * Retrieves counter last date from database.
   */
  public function getCounterLastDate() {
  public function getCounterLastDate($operator = '<>', $order = 'DESC') {
    $query = $this->database->select('counter', 'c');
    $query->condition('c.created', 0, '<>');
    $query->condition('c.created', 0, $operator);
    $query->fields('c', ['created']);
    $query->orderBy('created', 'DESC');
    $query->orderBy('created', $order);
    $query->range(0, 1);
    $result = $query->execute();

@@ -135,4 +135,73 @@ class CounterUtility {
      ->execute();
  }

  /**
   * Retrieves data from the counter data table.
   */
  public function getCounterDataTableData() {
    $query = $this->database->select('counter_data', 'c');
    $query->fields('c');
    $query->orderBy('counter_name');
    $result = $query->execute();
    $counter_data = [];
    foreach ($result as $record) {
      $counter_data[$record->counter_name] = $record->counter_value;
    }

    return $counter_data;
  }

  /**
   * Retrieves unique visitors from the counter table.
   */
  public function getUniqueVisitorData() {
    $query = $this->database->select('counter', 'c');
    $query->fields('c', ['ip']);
    $query->groupBy('c.ip');
    $result = $query->countQuery()->execute()->fetchField();

    return $result;
  }

  /**
   * Retrieves data related to users in the website.
   */
  public function getTotalUsers($operator = '<>', $status = 1) {
    $query = $this->database->select('users_field_data', 'u');
    $query->fields('u');
    $query->condition('u.access', 0, $operator);
    $query->condition('u.uid', 0, '<>');
    $query->condition('u.status', $status);
    $result = $query->countQuery()->execute()->fetchField();

    return $result;
  }

  /**
   * Retrieves data related to nodes in the website.
   */
  public function getTotalNodes($status = 1) {
    $query = $this->database->select('node_field_data', 'n');
    $query->fields('n');
    $query->condition('n.status', $status);
    $result = $query->countQuery()->execute()->fetchField();

    return $result;
  }

  /**
   * Retrieves data based on time span.
   */
  public function getTimeRangeData($date1, $date2 = 0) {
    $query = $this->database->select('counter', 'c');
    $query->fields('c');
    $query->condition('c.created', $date1, '>');
    if ($date2 != 0) {
      $query->condition('c.created', $date2, '<');
    }
    $result = $query->countQuery()->execute()->fetchField();

    return $result;
  }

}
+38 −140
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\counter\Plugin\Block;

use Drupal\counter\CounterUtility;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
@@ -48,6 +49,13 @@ class CounterBlock extends BlockBase implements ContainerFactoryPluginInterface
   */
  protected $account;

  /**
   * The counter utility variable for accessing helper functions.
   *
   * @var Drupal\counter\CounterUtility
   */
  protected $counterUtility;

  /**
   * Constructor for the counter block.
   *
@@ -65,13 +73,16 @@ class CounterBlock extends BlockBase implements ContainerFactoryPluginInterface
   *   The database connection variable for executing queries.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account interface for accessing current user.
   * @param \Drupal\counter\CounterUtility $counter_utility
   *   The counter utility variable to access helper functions.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, RequestStack $request_stack, Connection $database, AccountInterface $account) {
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, RequestStack $request_stack, Connection $database, AccountInterface $account, CounterUtility $counter_utility) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $config_factory;
    $this->requestStack = $request_stack;
    $this->database = $database;
    $this->account = $account;
    $this->counterUtility = $counter_utility;
  }

  /**
@@ -96,7 +107,8 @@ class CounterBlock extends BlockBase implements ContainerFactoryPluginInterface
      $container->get('config.factory'),
      $container->get('request_stack'),
      $container->get('database'),
      $container->get('current_user')
      $container->get('current_user'),
      $container->get('counter.counter_utility')
    );
  }

@@ -126,25 +138,11 @@ class CounterBlock extends BlockBase implements ContainerFactoryPluginInterface
    $ip = $this->requestStack->getCurrentRequest()->getClientIp();
    $counter_svr_ip = $this->requestStack->getCurrentRequest()->server->get('SERVER_ADDR');

    // Counter_insert_delay.
    $db_types = $this->database->driver();
    switch ($db_types) {
      case 'mssql':
        $sql = " SELECT TOP 1 created FROM {counter} WHERE created<>0 ORDER BY created DESC";
        break;

      case 'oracle':
        $sql = " SELECT created FROM {counter} WHERE ROWNUM=1 AND created<>0 ORDER BY created DESC";
        break;

      default:
        $sql = " SELECT created FROM {counter} WHERE created<>0 ORDER BY created DESC LIMIT 1";
    }

    $counter_lastdate = $this->database->query($sql)->fetchField();
    // Fetch last created value from database.
    $counter_lastdate = $this->counterUtility->getCounterLastDate();

    // Check if permitted to insert data.
    $interval = \time() - $counter_lastdate;
    // Check if permitted to display fresh data.
    $interval = time() - $counter_lastdate;
    $data_update = ($interval >= $counter_refresh_delay ? 1 : 0);

    // Read counter_data.
@@ -157,96 +155,37 @@ class CounterBlock extends BlockBase implements ContainerFactoryPluginInterface
      $counter_value[$i] = $data->counter_value;
    }

    $counter_data = $this->counterUtility->getCounterDataTableData();

    // Write output.
    $output = '<div  id="counter">';
    $output .= '<ul>';
    if ($counter_show_site_counter) {
      $output .= '<li>' . $this->t('Site Counter:') . '<strong>' . number_format($counter_initial_counter + $counter_value[4]) . '</strong></li>';
      $output .= '<li>' . $this->t('Site Counter:') . '<strong>' . number_format($counter_initial_counter + $counter_data['site_counter']) . '</strong></li>';
    }

    if ($counter_show_unique_visitor) {
      if ($data_update) {
        $sql = " SELECT count(*) as total " .
        " FROM (SELECT ip FROM {counter} GROUP BY ip) c";
        $counter_unique = $this->database->query($sql)->fetchField();

        $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='unique_visitor' ";
        $results = $this->database->query($sql, [':counter_value' => $counter_unique]);
      }
      else {
        $counter_unique = $counter_value[5];
      }
      $output .= '<li>' . $this->t('Unique Visitor:') . '<strong>' . number_format($counter_initial_unique_visitor + $counter_unique) . '</strong></li>';
    if ($counter_show_unique_visitor && $data_update) {
      $output .= '<li>' . $this->t('Unique Visitor:') . '<strong>' . number_format($counter_initial_unique_visitor + $this->counterUtility->getUniqueVisitorData()) . '</strong></li>';
    }

    if ($counter_registered_user) {
      if ($data_update) {
        $sql   = " SELECT count(*) as total FROM {users_field_data} WHERE access<>0 and uid<>0";
        $total = $this->database->query($sql)->fetchField();

        $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='registered_user' ";
        $results = $this->database->query($sql, [':counter_value' => $total]);
      }
      else {
        $total = $counter_value[3];
    if ($counter_registered_user && $data_update) {
      $output .= '<li>' . $this->t('Registered Users:') . '<strong>' . number_format($this->counterUtility->getTotalUser()) . '</strong></li>';
    }
      $output .= '<li>' . $this->t('Registered Users:') . '<strong>' . number_format($total) . '</strong></li>';
    }

    if ($counter_unregistered_user) {
      if ($data_update) {
        $sql   = " SELECT count(*) as total FROM {users_field_data} WHERE access=0 and uid<>0";
        $total = $this->database->query($sql)->fetchField();

        $sql = "UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='unregistered_user' ";
        $results = $this->database->query($sql, [':counter_value' => $total]);
      }
      else {
        $total = $counter_value[7];
      }
      $output .= '<li>' . $this->t('Unregistered Users:') . '<strong>' . number_format($total) . '</strong></li>';
    if ($counter_unregistered_user && $data_update) {
      $output .= '<li>' . $this->t('Unregistered Users:') . '<strong>' . number_format($this->counterUtility->getTotalUser('=')) . '</strong></li>';
    }

    if ($counter_blocked_user) {
      if ($data_update) {
        $sql   = " SELECT count(*) as total FROM {users_field_data} WHERE status=0 and uid<>0";
        $total = $this->database->query($sql)->fetchField();

        $sql = " UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='blocked_user' ";
        $results = $this->database->query($sql, [':counter_value' => $total]);
      }
      else {
        $total = $counter_value[1];
    if ($counter_blocked_user && $data_update) {
      $output .= '<li>' . $this->t('Blocked Users:') . '<strong>' . number_format($this->counterUtility->getTotalUser('=', 0)) . '</strong></li>';
    }
      $output .= '<li>' . $this->t('Blocked Users:') . '<strong>' . number_format($total) . '</strong></li>';
    }

    if ($counter_published_node) {
      if ($data_update) {
        $sql   = " SELECT count(*) as total FROM {node_field_data} WHERE status=1";
        $total = $this->database->query($sql)->fetchField();

        $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='published_node' ";
        $results = $this->database->query($sql, [':counter_value' => $total]);
      }
      else {
        $total = $counter_value[2];
      }
      $output .= '<li>' . $this->t('Published Nodes:') . '<strong>' . number_format($total) . '</strong></li>';
    if ($counter_published_node && $data_update) {
      $output .= '<li>' . $this->t('Published Nodes:') . '<strong>' . number_format($this->counterUtility->getTotalNodes()) . '</strong></li>';
    }

    if ($counter_unpublished_node) {
      if ($data_update) {
        $sql   = " SELECT count(*) as total FROM {node_field_data} WHERE status=0";
        $total = $this->database->query($sql)->fetchField();

        $sql = "UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='unpublished_node' ";
        $results = $this->database->query($sql, [':counter_value' => $total]);
      }
      else {
        $total = $counter_value[6];
      }
      $output .= '<li>' . $this->t('Unpublished Nodes:') . '<strong>' . number_format($total) . '</strong></li>';
    if ($counter_unpublished_node && $data_update) {
      $output .= '<li>' . $this->t('Unpublished Nodes:') . '<strong>' . number_format($this->counterUtility->getTotalNodes(0)) . '</strong></li>';
    }

    if ($counter_show_server_ip) {
@@ -258,20 +197,7 @@ class CounterBlock extends BlockBase implements ContainerFactoryPluginInterface
    }

    if ($counter_show_counter_since) {
      switch ($db_types) {
        case 'mssql':
          $sql = " SELECT TOP 1 created FROM {counter} WHERE created>0 ORDER BY created ASC";
          break;

        case 'oracle':
          $sql = " SELECT created FROM {counter} WHERE ROWNUM=1 AND created>0 ORDER BY created ASC";
          break;

        default:
          $sql = " SELECT created FROM {counter} WHERE created>0 ORDER BY created ASC LIMIT 1";
      }

      $counter_since = $this->database->query($sql)->fetchField();
      $counter_since = $this->counterUtility->getCounterLastDate('>', 'ASC');

      if ($counter_initial_since <> 0) {
        $counter_since = $counter_initial_since;
@@ -286,44 +212,16 @@ class CounterBlock extends BlockBase implements ContainerFactoryPluginInterface
    $output .= '<ul>';

    if ($counter_statistic_today) {
      $date1 = strtotime(date('Y-m-d'));
      $sql = "SELECT count(*) AS total FROM {counter} WHERE created >= :counter_stat_today";
      $results = $this->database->query($sql, [':counter_stat_today' => $date1]);
      $statistic = $results->fetchField();
      $output .= '<li>' . $this->t("Today:") . '<strong>' . number_format($statistic) . "</strong></li>";
      $output .= '<li>' . $this->t("Today:") . '<strong>' . number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d')))) . "</strong></li>";
    }
    if ($counter_statistic_week) {
      $date1 = strtotime(date('Y-m-d')) - 7 * 24 * 60 * 60;
      $date2 = time();
      $sql = " SELECT count(*) AS total FROM {counter} WHERE created > :date1 AND created <= :date2";
      $results = $this->database->query($sql, [
        ':date1' => $date1,
        ':date2' => $date2,
      ]);
      $statistic = $results->fetchField();
      $output .= '<li>' . $this->t("This week:") . '<strong>' . number_format($statistic) . "</strong></li>";
      $output .= '<li>' . $this->t("This week:") . '<strong>' . number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d')) - 7 * 24 * 60 * 60, time())) . "</strong></li>";
    }
    if ($counter_statistic_month) {
      $date1 = strtotime(date('Y-m-d')) - 30 * 24 * 60 * 60;
      $date2 = time();
      $sql = " SELECT count(*) AS total FROM {counter} WHERE created > :date1 AND created <= :date2";
      $results = $this->database->query($sql, [
        ':date1' => $date1,
        ':date2' => $date2,
      ]);
      $statistic = $results->fetchField();
      $output .= '<li>' . $this->t("This month:") . '<strong>' . number_format($statistic) . "</strong></li>";
      $output .= '<li>' . $this->t("This month:") . '<strong>' . number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d')) - 30 * 24 * 60 * 60, time())) . "</strong></li>";
    }
    if ($counter_statistic_year) {
      $date1 = strtotime(date('Y-m-d')) - 365 * 24 * 60 * 60;
      $date2 = time();
      $sql = " SELECT count(*) AS total FROM {counter} WHERE created > :date1 AND created <= :date2";
      $results = $this->database->query($sql, [
        ':date1' => $date1,
        ':date2' => $date2,
      ]);
      $statistic = $results->fetchField();
      $output .= '<li>' . $this->t("This year:") . '<strong>' . number_format($statistic) . '</strong></li>';
      $output .= '<li>' . $this->t("This year:") . '<strong>' . number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d')) - 365 * 24 * 60 * 60, time())) . '</strong></li>';
    }

    $output .= '</ul>';