You are here

class CounterBlock in Counter 8

Provides a 'counter' block.

Plugin annotation


@Block(
  id = "counter_block",
  admin_label = @Translation("Counter"),
)

Hierarchy

Expanded class hierarchy of CounterBlock

File

src/Plugin/Block/CounterBlock.php, line 18

Namespace

Drupal\counter\Plugin\Block
View source
class CounterBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account) {
    return AccessResult::allowedIfHasPermission($account, 'administer counter');
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $config = \Drupal::config('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');
    $counter_unregistered_user = $config
      ->get('counter_unregistered_user');
    $counter_blocked_user = $config
      ->get('counter_blocked_user');
    $counter_published_node = $config
      ->get('counter_published_node');
    $counter_unpublished_node = $config
      ->get('counter_unpublished_node');
    $counter_show_server_ip = $config
      ->get('counter_show_server_ip');
    $counter_show_ip = $config
      ->get('counter_show_ip');
    $counter_show_counter_since = $config
      ->get('counter_show_counter_since');
    $counter_skip_admin = $config
      ->get('counter_skip_admin');
    $counter_refresh_delay = $config
      ->get('counter_refresh_delay');
    $counter_insert_delay = $config
      ->get('counter_insert_delay');
    $counter_initial_counter = $config
      ->get('counter_initial_counter');
    $counter_initial_unique_visitor = $config
      ->get('counter_initial_unique_visitor');
    $counter_initial_since = $config
      ->get('counter_initial_since');
    $counter_statistic_today = $config
      ->get('counter_statistic_today');
    $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'];
    $created = \time();
    $url = SafeMarkup::checkPlain(\Drupal::request()
      ->getRequestUri());

    // Counter_insert_delay.
    $db_types = db_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 = db_query($sql)
      ->fetchField();

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

    // Uid, nid, type, browser name, browser version, platform.
    $node = \Drupal::request()->attributes
      ->get('node');
    $nid = 0;
    $type = '';
    $account = \Drupal::currentUser();
    $path_args = \explode('/', \Drupal::request()
      ->getRequestUri());
    if ($path_args[0] == 'node' && is_numeric($path_args[1])) {
      $nid = $node->nid;
      $type = $node->type;
    }
    module_load_include('inc', 'counter', 'counter.lib');
    $browser = counter_get_browser();
    $browser_name = $browser['browser_name'];
    $browser_version = $browser['browser_version'];
    $platform = $browser['platform'];
    $sql = " INSERT INTO {counter} (ip,created,url, uid, nid, type, browser_name, browser_version, platform)" . " VALUES (:ip, :created, :url, :uid, :nid, :type, :browser_name, :browser_version, :platform)";
    $counter_exec = FALSE;
    if ($data_insert && $account
      ->id() != 1) {
      $counter_exec = TRUE;
    }
    else {
      if ($data_insert && $account
        ->id() == 1 && !$counter_skip_admin) {
        $counter_exec = TRUE;
      }
    }
    if ($counter_exec) {
      $results = db_query($sql, array(
        ':ip' => $ip,
        ':created' => $created,
        ':url' => $url,
        ':uid' => $account
          ->id(),
        ':nid' => $nid,
        ':type' => $type,
        ':browser_name' => $browser_name,
        ':browser_version' => $browser_version,
        ':platform' => $platform,
      ));
    }
    else {
      return;
    }
    $sql_site_counter = "SELECT counter_value FROM {counter_data} WHERE counter_name='site_counter'";
    $site_counter = db_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 = db_query($sql, array(
      ':counter_value' => $new_site_counter,
    ));

    // Read counter_data.
    $sql = " SELECT * FROM {counter_data} ORDER BY counter_name";
    $results = db_query($sql);
    $i = 0;
    foreach ($results as $data) {
      $i++;
      $counter_value[$i] = $data->counter_value;
    }

    // Write output.
    $output = '<div  id="counter">';
    $output .= '<ul>';
    if ($counter_show_site_counter) {
      $output .= '<li>' . t('Site Counter:') . '<strong>' . number_format($counter_initial_counter + $counter_value[4]) . '</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 = db_query($sql)
          ->fetchField();
        $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='unique_visitor' ";
        $results = db_query($sql, array(
          ':counter_value' => $counter_unique,
        ));
      }
      else {
        $counter_unique = $counter_value[5];
      }
      $output .= '<li>' . t('Unique Visitor:') . '<strong>' . number_format($counter_initial_unique_visitor + $counter_unique) . '</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 = db_query($sql)
          ->fetchField();
        $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='registered_user' ";
        $results = db_query($sql, array(
          ':counter_value' => $total,
        ));
      }
      else {
        $total = $counter_value[3];
      }
      $output .= '<li>' . 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 = db_query($sql)
          ->fetchField();
        $sql = "UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='unregistered_user' ";
        $results = db_query($sql, array(
          ':counter_value' => $total,
        ));
      }
      else {
        $total = $counter_value[7];
      }
      $output .= '<li>' . t('Unregistered Users:') . '<strong>' . number_format($total) . '</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 = db_query($sql)
          ->fetchField();
        $sql = " UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='blocked_user' ";
        $results = db_query($sql, array(
          ':counter_value' => $total,
        ));
      }
      else {
        $total = $counter_value[1];
      }
      $output .= '<li>' . 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 = db_query($sql)
          ->fetchField();
        $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='published_node' ";
        $results = db_query($sql, array(
          ':counter_value' => $total,
        ));
      }
      else {
        $total = $counter_value[2];
      }
      $output .= '<li>' . t('Published Nodes:') . '<strong>' . number_format($total) . '</strong></li>';
    }
    if ($counter_unpublished_node) {
      if ($data_update) {
        $sql = " SELECT count(*) as total FROM {node_field_data} WHERE status=0";
        $total = db_query($sql)
          ->fetchField();
        $sql = "UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='unpublished_node' ";
        $results = db_query($sql, array(
          ':counter_value' => $total,
        ));
      }
      else {
        $total = $counter_value[6];
      }
      $output .= '<li>' . t('Unpublished Nodes:') . '<strong>' . number_format($total) . '</strong></li>';
    }
    if ($counter_show_server_ip) {
      $output .= '<li>' . t("Server IP:") . '<strong>' . $counter_svr_ip . '</strong></li>';
    }
    if ($counter_show_ip) {
      $output .= '<li>' . t("Your IP:") . '<strong>' . $ip . '</strong></li>';
    }
    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 = db_query($sql)
        ->fetchField();
      if ($counter_initial_since != 0) {
        $counter_since = $counter_initial_since;
      }
      $output .= '<li>' . t("Since:") . '<strong>' . date('d M Y', $counter_since) . '</strong></li>';
    }
    if ($counter_statistic_today || $counter_statistic_week || $counter_statistic_month || $counter_statistic_year) {
      $output .= '<li>' . t("Visitors:") . '</li>';
    }
    $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 = db_query($sql, array(
        ':counter_stat_today' => $date1,
      ));
      $statistic = $results
        ->fetchField();
      $output .= '<li>' . t("Today:") . '<strong>' . number_format($statistic) . "</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 = db_query($sql, array(
        ':date1' => $date1,
        ':date2' => $date2,
      ));
      $statistic = $results
        ->fetchField();
      $output .= '<li>' . t("This week:") . '<strong>' . number_format($statistic) . "</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 = db_query($sql, array(
        ':date1' => $date1,
        ':date2' => $date2,
      ));
      $statistic = $results
        ->fetchField();
      $output .= '<li>' . t("This month:") . '<strong>' . number_format($statistic) . "</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 = db_query($sql, array(
        ':date1' => $date1,
        ':date2' => $date2,
      ));
      $statistic = $results
        ->fetchField();
      $output .= '<li>' . t("This year:") . '<strong>' . number_format($statistic) . '</strong></li>';
    }
    $output .= '</ul>';
    $output .= '</ul>';
    $output .= '</div>';
    return [
      '#attached' => [
        'library' => array(
          'counter/counter.custom',
        ),
      ],
      '#markup' => $output,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockPluginInterface::BLOCK_LABEL_VISIBLE constant Indicates the block label (title) should be displayed to end users.
BlockPluginTrait::$transliteration protected property The transliteration service.
BlockPluginTrait::access public function
BlockPluginTrait::baseConfigurationDefaults protected function Returns generic default configuration for block plugins.
BlockPluginTrait::blockForm public function 16
BlockPluginTrait::blockSubmit public function 13
BlockPluginTrait::blockValidate public function 3
BlockPluginTrait::buildConfigurationForm public function Creates a generic configuration form for all block types. Individual block plugins can add elements to this form by overriding BlockBase::blockForm(). Most block plugins should not override this method unless they need to alter the generic form elements. 2
BlockPluginTrait::calculateDependencies public function
BlockPluginTrait::defaultConfiguration public function 19
BlockPluginTrait::getConfiguration public function 1
BlockPluginTrait::getMachineNameSuggestion public function 1
BlockPluginTrait::getPreviewFallbackString public function 3
BlockPluginTrait::label public function
BlockPluginTrait::setConfiguration public function
BlockPluginTrait::setConfigurationValue public function
BlockPluginTrait::setTransliteration public function Sets the transliteration service.
BlockPluginTrait::submitConfigurationForm public function Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit().
BlockPluginTrait::transliteration protected function Wraps the transliteration service.
BlockPluginTrait::validateConfigurationForm public function Most block plugins should not override this method. To add validation for a specific block type, override BlockBase::blockValidate(). 1
BlockPluginTrait::__construct public function 22
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::$contexts Deprecated private property Data objects representing the contexts passed in the plugin configuration.
ContextAwarePluginBase::createContextFromConfiguration protected function Overrides ContextAwarePluginBase::createContextFromConfiguration
ContextAwarePluginBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts 9
ContextAwarePluginBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge 7
ContextAwarePluginBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags 4
ContextAwarePluginBase::getContext public function This code is identical to the Component in order to pick up a different Context class. Overrides ContextAwarePluginBase::getContext
ContextAwarePluginBase::getContextDefinition public function Overrides ContextAwarePluginBase::getContextDefinition
ContextAwarePluginBase::getContextDefinitions public function Overrides ContextAwarePluginBase::getContextDefinitions
ContextAwarePluginBase::getContextMapping public function Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::getContextMapping
ContextAwarePluginBase::getContexts public function Gets the defined contexts. Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Gets the value for a defined context. Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Gets the values for all defined contexts. Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContext public function Set a context on this plugin. Overrides ContextAwarePluginBase::setContext
ContextAwarePluginBase::setContextMapping public function Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::setContextMapping
ContextAwarePluginBase::setContextValue public function Sets the value for a defined context. Overrides ContextAwarePluginBase::setContextValue
ContextAwarePluginBase::validateContexts public function Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__get public function Implements magic __get() method.
CounterBlock::blockAccess protected function Indicates whether the block should be shown. Overrides BlockPluginTrait::blockAccess
CounterBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginWithFormsTrait::getFormClass public function
PluginWithFormsTrait::hasFormClass public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2