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

Issue #3264197 by gaurav.kapoor: Fix CS issues, internal documentation, remove...

Issue #3264197 by gaurav.kapoor: Fix CS issues, internal documentation, remove deprecated functions and refactor code
parent 356162f6
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ function counter_help($route_name, RouteMatchInterface $route_match) {
/**
 * To find out browser type and return browser info.
 */

function counter_get_browser() {
  $user_agent      = $_SERVER['HTTP_USER_AGENT'];
  $browser_name    = 'Unknown';
+126 −32
Original line number Diff line number Diff line
@@ -4,8 +4,13 @@ namespace Drupal\counter\Plugin\Block;

use Drupal\Component\Utility\Html;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Provides a 'counter' block.
@@ -15,7 +20,87 @@ use Drupal\Core\Block\BlockBase;
 *   admin_label = @Translation("Counter"),
 * )
 */
class CounterBlock extends BlockBase {
class CounterBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The request stack.
   *
   * @var Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The database connection.
   *
   * @var Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The account interface for accessing current user.
   *
   * @var Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * Constructor for the counter block.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The plugin ID of the block.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory for the form.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack for accessing request information.
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection variable for executing queries.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account interface for accessing current user.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, RequestStack $request_stack, Connection $database, AccountInterface $account) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $config_factory;
    $this->requestStack = $request_stack;
    $this->database = $database;
    $this->account = $account;
  }

  /**
   * Create function to assemble the counter block.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The container interface.
   * @param array $configuration
   *   The block plugin configuration.
   * @param string $plugin_id
   *   The plugin ID of the block.
   * @param mixed $plugin_definition
   *   Definition of the plugin block.
   *
   * @return static
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('config.factory'),
      $container->get('request_stack'),
      $container->get('database'),
      $container->get('current_user')
    );
  }

  /**
   * {@inheritdoc}
@@ -29,7 +114,7 @@ class CounterBlock extends BlockBase {
   */
  public function build() {

    $config                         = \Drupal::config('counter.settings');
    $config                         = $this->configFactory->get('counter.settings');
    $counter_show_site_counter      = $config->get('counter_show_site_counter');
    $counter_show_unique_visitor    = $config->get('counter_show_unique_visitor');
    $counter_registered_user        = $config->get('counter_registered_user');
@@ -50,13 +135,13 @@ class CounterBlock extends BlockBase {
    $counter_statistic_week         = $config->get('counter_statistic_week');
    $counter_statistic_month        = $config->get('counter_statistic_month');
    $counter_statistic_year         = $config->get('counter_statistic_year');
    $ip                             = \Drupal::request()->getClientIp();
    $counter_svr_ip                 = $_SERVER['SERVER_ADDR'];
    $ip                             = $this->requestStack->getClientIp();
    $counter_svr_ip                 = $this->requestStack->server->get('SERVER_ADDR');
    $created                        = \time();
    $url                            = Html::escape(\Drupal::request()->getRequestUri());
    $url                            = Html::escape($this->requestStack->getRequestUri());

    // Counter_insert_delay.
    $db_types = \Drupal::database()->driver();
    $db_types = $this->database->driver();
    switch ($db_types) {
      case 'mssql':
        $sql = " SELECT TOP 1 created FROM {counter} WHERE created<>0 ORDER BY created DESC";
@@ -70,7 +155,7 @@ class CounterBlock extends BlockBase {
        $sql = " SELECT created FROM {counter} WHERE created<>0 ORDER BY created DESC LIMIT 1";
    }

    $counter_lastdate = \Drupal::database()->query($sql)->fetchField();
    $counter_lastdate = $this->database->query($sql)->fetchField();

    // Check if permited to insert data.
    $interval = \time() - $counter_lastdate;
@@ -78,11 +163,11 @@ class CounterBlock extends BlockBase {
    $data_update = ($interval >= $counter_refresh_delay ? 1 : 0);

    // Uid, nid, type, browser name, browser version, platform.
    $node = \Drupal::request()->attributes->get('node');
    $node = $this->requestStack->attributes->get('node');
    $nid = 0;
    $type = '';
    $account = \Drupal::currentUser();
    $path_args = \explode('/', \Drupal::request()->getRequestUri());
    $account = $this->account;
    $path_args = \explode('/', $this->requestStack->getRequestUri());
    if ($path_args[0] == 'node' && is_numeric($path_args[1])) {
      $nid  = $node->nid;
      $type = $node->type;
@@ -107,7 +192,7 @@ class CounterBlock extends BlockBase {
    }

    if ($counter_exec) {
      $results = \Drupal::database()->query($sql, [
      $results = $this->database->query($sql, [
        ':ip' => $ip,
        ':created' => $created,
        ':url' => $url,
@@ -124,16 +209,16 @@ class CounterBlock extends BlockBase {
    }

    $sql_site_counter = "SELECT counter_value FROM {counter_data} WHERE counter_name='site_counter'";
    $site_counter     = \Drupal::database()->query($sql_site_counter)->fetchField();
    $site_counter     = $this->database->query($sql_site_counter)->fetchField();

    $new_site_counter = $site_counter + 1;

    $sql = " UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='site_counter'";
    $results = \Drupal::database()->query($sql, [':counter_value' => $new_site_counter]);
    $results = $this->database->query($sql, [':counter_value' => $new_site_counter]);

    // Read counter_data.
    $sql = " SELECT * FROM {counter_data} ORDER BY counter_name";
    $results = \Drupal::database()->query($sql);
    $results = $this->database->query($sql);
    $i = 0;

    foreach ($results as $data) {
@@ -152,10 +237,10 @@ class CounterBlock extends BlockBase {
      if ($data_update) {
        $sql = " SELECT count(*) as total " .
        " FROM (SELECT ip FROM {counter} GROUP BY ip) c";
        $counter_unique = \Drupal::database()->query($sql)->fetchField();
        $counter_unique = $this->database->query($sql)->fetchField();

        $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='unique_visitor' ";
        $results = \Drupal::database()->query($sql, [':counter_value' => $counter_unique]);
        $results = $this->database->query($sql, [':counter_value' => $counter_unique]);
      }
      else {
        $counter_unique = $counter_value[5];
@@ -166,10 +251,10 @@ class CounterBlock extends BlockBase {
    if ($counter_registered_user) {
      if ($data_update) {
        $sql   = " SELECT count(*) as total FROM {users_field_data} WHERE access<>0 and uid<>0";
        $total = \Drupal::database()->query($sql)->fetchField();
        $total = $this->database->query($sql)->fetchField();

        $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='registered_user' ";
        $results = \Drupal::database()->query($sql, [':counter_value' => $total]);
        $results = $this->database->query($sql, [':counter_value' => $total]);
      }
      else {
        $total = $counter_value[3];
@@ -180,10 +265,10 @@ class CounterBlock extends BlockBase {
    if ($counter_unregistered_user) {
      if ($data_update) {
        $sql   = " SELECT count(*) as total FROM {users_field_data} WHERE access=0 and uid<>0";
        $total = \Drupal::database()->query($sql)->fetchField();
        $total = $this->database->query($sql)->fetchField();

        $sql = "UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='unregistered_user' ";
        $results = \Drupal::database()->query($sql, [':counter_value' => $total]);
        $results = $this->database->query($sql, [':counter_value' => $total]);
      }
      else {
        $total = $counter_value[7];
@@ -194,10 +279,10 @@ class CounterBlock extends BlockBase {
    if ($counter_blocked_user) {
      if ($data_update) {
        $sql   = " SELECT count(*) as total FROM {users_field_data} WHERE status=0 and uid<>0";
        $total = \Drupal::database()->query($sql)->fetchField();
        $total = $this->database->query($sql)->fetchField();

        $sql = " UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='blocked_user' ";
        $results = \Drupal::database()->query($sql, [':counter_value' => $total]);
        $results = $this->database->query($sql, [':counter_value' => $total]);
      }
      else {
        $total = $counter_value[1];
@@ -208,10 +293,10 @@ class CounterBlock extends BlockBase {
    if ($counter_published_node) {
      if ($data_update) {
        $sql   = " SELECT count(*) as total FROM {node_field_data} WHERE status=1";
        $total = \Drupal::database()->query($sql)->fetchField();
        $total = $this->database->query($sql)->fetchField();

        $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='published_node' ";
        $results = \Drupal::database()->query($sql, [':counter_value' => $total]);
        $results = $this->database->query($sql, [':counter_value' => $total]);
      }
      else {
        $total = $counter_value[2];
@@ -222,10 +307,10 @@ class CounterBlock extends BlockBase {
    if ($counter_unpublished_node) {
      if ($data_update) {
        $sql   = " SELECT count(*) as total FROM {node_field_data} WHERE status=0";
        $total = \Drupal::database()->query($sql)->fetchField();
        $total = $this->database->query($sql)->fetchField();

        $sql = "UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='unpublished_node' ";
        $results = \Drupal::database()->query($sql, [':counter_value' => $total]);
        $results = $this->database->query($sql, [':counter_value' => $total]);
      }
      else {
        $total = $counter_value[6];
@@ -255,7 +340,7 @@ class CounterBlock extends BlockBase {
          $sql = " SELECT created FROM {counter} WHERE created>0 ORDER BY created ASC LIMIT 1";
      }

      $counter_since = \Drupal::database()->query($sql)->fetchField();
      $counter_since = $this->database->query($sql)->fetchField();

      if ($counter_initial_since <> 0) {
        $counter_since = $counter_initial_since;
@@ -272,7 +357,7 @@ class CounterBlock extends BlockBase {
    if ($counter_statistic_today) {
      $date1 = strtotime(date('Y-m-d'));
      $sql = "SELECT count(*) AS total FROM {counter} WHERE created >= :counter_stat_today";
      $results = \Drupal::database()->query($sql, [':counter_stat_today' => $date1]);
      $results = $this->database->query($sql, [':counter_stat_today' => $date1]);
      $statistic = $results->fetchField();
      $output .= '<li>' . $this->t("Today:") . '<strong>' . number_format($statistic) . "</strong></li>";
    }
@@ -280,7 +365,10 @@ class CounterBlock extends BlockBase {
      $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 = \Drupal::database()->query($sql, [':date1' => $date1, ':date2' => $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>";
    }
@@ -288,7 +376,10 @@ class CounterBlock extends BlockBase {
      $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 = \Drupal::database()->query($sql, [':date1' => $date1, ':date2' => $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>";
    }
@@ -296,7 +387,10 @@ class CounterBlock extends BlockBase {
      $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 = \Drupal::database()->query($sql, [':date1' => $date1, ':date2' => $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>';
    }