Commit 62bac4e6 authored by Gaurav Kapoor's avatar Gaurav Kapoor Committed by Gaurav Kapoor
Browse files

Issue #3264691 by gaurav.kapoor: Add template file for counter block

parent 24431ee0
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -19,3 +19,29 @@ function counter_help($route_name, RouteMatchInterface $route_match) {
      return $output;
  }
}

/**
 * Implements hook_theme().
 */
function counter_theme($existing, $type, $theme, $path) {
  return [
    'counter' => [
      'variables' => [
        'site_counter' => NULL,
        'unique_visitor' => NULL,
        'registered_user' => NULL,
        'unregistered_user' => NULL,
        'blocked_user' => NULL,
        'published_nodes' => NULL,
        'unpublished_nodes' => NULL,
        'server_ip' => NULL,
        'ip' => NULL,
        'counter_since' => NULL,
        'statistic_today' => NULL,
        'statistic_week' => NULL,
        'statistic_month' => NULL,
        'statistic_year' => NULL,
      ],
    ],
  ];
}
+36 −45
Original line number Diff line number Diff line
@@ -115,7 +115,6 @@ class CounterBlock extends BlockBase implements ContainerFactoryPluginInterface
    $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_refresh_delay = $config->get('counter_refresh_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');
@@ -123,95 +122,87 @@ class CounterBlock extends BlockBase implements ContainerFactoryPluginInterface
    $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 = $this->requestStack->getCurrentRequest()->getClientIp();
    $counter_svr_ip = $this->requestStack->getCurrentRequest()->server->get('SERVER_ADDR');

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

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

    // Read counter_data.
    $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_data['site_counter']) . '</strong></li>';
      $site_counter = number_format($counter_initial_counter + $counter_data['site_counter']);
    }

    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_show_unique_visitor) {
      $unique_visitor = number_format($counter_initial_unique_visitor + $this->counterUtility->getUniqueVisitorData());
    }

    if ($counter_registered_user && $data_update) {
      $output .= '<li>' . $this->t('Registered Users:') . '<strong>' . number_format($this->counterUtility->getTotalUser()) . '</strong></li>';
    if ($counter_registered_user) {
      $registered_user = number_format($this->counterUtility->getTotalUsers());
    }

    if ($counter_unregistered_user && $data_update) {
      $output .= '<li>' . $this->t('Unregistered Users:') . '<strong>' . number_format($this->counterUtility->getTotalUser('=')) . '</strong></li>';
    if ($counter_unregistered_user) {
      $unregistered_user = number_format($this->counterUtility->getTotalUsers('='));
    }

    if ($counter_blocked_user && $data_update) {
      $output .= '<li>' . $this->t('Blocked Users:') . '<strong>' . number_format($this->counterUtility->getTotalUser('=', 0)) . '</strong></li>';
    if ($counter_blocked_user) {
      $blocked_user = number_format($this->counterUtility->getTotalUsers('=', 0));
    }

    if ($counter_published_node && $data_update) {
      $output .= '<li>' . $this->t('Published Nodes:') . '<strong>' . number_format($this->counterUtility->getTotalNodes()) . '</strong></li>';
    if ($counter_published_node) {
      $published_nodes = number_format($this->counterUtility->getTotalNodes());
    }

    if ($counter_unpublished_node && $data_update) {
      $output .= '<li>' . $this->t('Unpublished Nodes:') . '<strong>' . number_format($this->counterUtility->getTotalNodes(0)) . '</strong></li>';
    if ($counter_unpublished_node) {
      $unpublished_nodes = number_format($this->counterUtility->getTotalNodes(0));
    }

    if ($counter_show_server_ip) {
      $output .= '<li>' . $this->t("Server IP:") . '<strong>' . $counter_svr_ip . '</strong></li>';
      $server_ip = $this->requestStack->getCurrentRequest()->server->get('SERVER_ADDR');
    }

    if ($counter_show_ip) {
      $output .= '<li>' . $this->t("Your IP:") . '<strong>' . $ip . '</strong></li>';
      $ip = $this->requestStack->getCurrentRequest()->getClientIp();
    }

    if ($counter_show_counter_since) {
      $counter_since = $this->counterUtility->getCounterLastDate('>', 'ASC');

      if ($counter_initial_since <> 0) {
      if ($counter_initial_since != 0) {
        $counter_since = $counter_initial_since;
      }
      $output .= '<li>' . $this->t("Since:") . '<strong>' . date('d M Y', $counter_since) . '</strong></li>';
      $counter_since = date('d M Y', $counter_since);
    }

    if ($counter_statistic_today || $counter_statistic_week || $counter_statistic_month || $counter_statistic_year) {
      $output .= '<li>' . $this->t("Visitors:") . '</li>';
    }

    $output .= '<ul>';

    if ($counter_statistic_today) {
      $output .= '<li>' . $this->t("Today:") . '<strong>' . number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d')))) . "</strong></li>";
      $statistic_today = number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d'))));
    }
    if ($counter_statistic_week) {
      $output .= '<li>' . $this->t("This week:") . '<strong>' . number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d')) - 7 * 24 * 60 * 60, time())) . "</strong></li>";
      $statistic_week = number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d')) - 7 * 24 * 60 * 60, time()));
    }
    if ($counter_statistic_month) {
      $output .= '<li>' . $this->t("This month:") . '<strong>' . number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d')) - 30 * 24 * 60 * 60, time())) . "</strong></li>";
      $statistic_month = number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d')) - 30 * 24 * 60 * 60, time()));
    }
    if ($counter_statistic_year) {
      $output .= '<li>' . $this->t("This year:") . '<strong>' . number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d')) - 365 * 24 * 60 * 60, time())) . '</strong></li>';
      $statistic_year = number_format($this->counterUtility->getTimeRangeData(strtotime(date('Y-m-d')) - 365 * 24 * 60 * 60, time()));
    }

    $output .= '</ul>';
    $output .= '</ul>';
    $output .= '</div>';

    return [
      '#attached' => [
        'library' => ['counter/counter.custom'],
      ],
      '#markup' => $output,
      '#theme' => 'counter',
      '#site_counter' => $site_counter,
      '#unique_visitor' => $unique_visitor,
      '#registered_user' => $registered_user,
      '#unregistered_user' => $unregistered_user,
      '#blocked_user' => $blocked_user,
      '#published_nodes' => $published_nodes,
      '#unpublished_nodes' => $unpublished_nodes,
      '#server_ip' => $server_ip,
      '#ip' => $ip,
      '#counter_since' => $counter_since,
      '#statistic_today' => $statistic_today,
      '#statistic_week' => $statistic_week,
      '#statistic_month' => $statistic_month,
      '#statistic_year' => $statistic_year,
    ];
  }

+53 −0
Original line number Diff line number Diff line
{# counter/templates/counter.html.twig #}
<div id="counter">
  <ul>
    {% if site_counter is not null %}
    <li>{{ 'Site Counter:'|t }} <strong>{{ site_counter }}</strong></li>
    {% endif %}
    {% if unique_visitor is not null %}
    <li>{{ 'Unique Visitor:'|t }} <strong>{{ unique_visitor }}</strong></li>
    {% endif %}
    {% if registered_user is not null %}
    <li>{{ 'Registered Users:'|t }} <strong>{{ registered_user }}</strong></li>
    {% endif %}
    {% if unregistered_user is not null %}
    <li>{{ 'Unregistered Users:'|t }} <strong>{{ unregistered_user }}</strong></li>
    {% endif %}
    {% if blocked_user is not null %}
    <li>{{ 'Blocked Users:'|t }} <strong>{{ blocked_user }}</strong></li>
    {% endif %}
    {% if published_nodes is not null %}
    <li>{{ 'Published Nodes:'|t }} <strong>{{ published_nodes }}</strong></li>
    {% endif %}
    {% if unpublished_nodes is not null %}
    <li>{{ 'Unublished Nodes:'|t }} <strong>{{ unpublished_nodes }}</strong></li>
    {% endif %}
    {% if server_ip is not null %}
    <li>{{ 'Server IP Address:'|t }} <strong>{{ server_ip }}</strong></li>
    {% endif %}
    {% if ip is not null %}
    <li>{{ 'IP Address:'|t }} <strong>{{ ip }}</strong></li>
    {% endif %}
    {% if counter_since is not null %}
    <li>{{ 'Counter Since:'|t }} <strong>{{ counter_since }}</strong></li>
    {% endif %}
    {% if (statistic_today is not null) or (statistic_week is not null) or (statistic_month is not null) or (statistic_year is not null) %}
      <li> {{ 'Visitors'|t }}
        <ul>
          {% if statistic_today is not null %}
            <li>{{ 'Today:'|t }} <strong>{{ statistic_today }}</strong></li>
          {% endif %}
          {% if statistic_week is not null %}
            <li>{{ 'This week:'|t }} <strong>{{ statistic_week }}</strong></li>
          {% endif %}
          {% if statistic_month is not null %}
            <li>{{ 'This month:'|t }} <strong>{{ statistic_month }}</strong></li>
          {% endif %}
          {% if statistic_year is not null %}
            <li>{{ 'This year:'|t }} <strong>{{ statistic_year }}</strong></li>
          {% endif %}
        </ul>
      </li>
    {% endif %}
  </ul>
<div>