Loading src/Form/Settings.php +89 −96 Original line number Diff line number Diff line Loading @@ -88,92 +88,12 @@ class Settings extends ConfigFormBase { $system_config = $this->config('system.theme'); $form = parent::buildForm($form, $form_state); $form['settings'] = [ '#type' => 'fieldset', '#weight' => -30, '#title' => $this->t('Visitors block'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#description' => $this->t('Visitors block settings'), ]; $form['settings']['show_total_visitors'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Total Visitors'), '#default_value' => $config->get('show_total_visitors'), '#description' => $this->t('Show Total Visitors.'), ]; $form['settings']['start_count_total_visitors'] = [ '#type' => 'textfield', '#title' => $this->t('Total visitors start count'), '#default_value' => $config->get('start_count_total_visitors'), '#description' => $this->t('Start the count of the total visitors at this number. Useful for including the known number of visitors in the past.'), ]; $form['settings']['show_unique_visitor'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Unique Visitors'), '#default_value' => $config->get('show_unique_visitor'), '#description' => $this->t('Show Unique Visitors based on their IP.'), ]; $form['settings']['show_registered_users_count'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Registered Users Count'), '#default_value' => $config->get('show_registered_users_count'), '#description' => $this->t('Show Registered Users.'), ]; $form['settings']['show_last_registered_user'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Last Registered User'), '#default_value' => $config->get('show_last_registered_user'), '#description' => $this->t('Show Last Registered User.'), ]; $form['settings']['show_published_nodes'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Published Nodes'), '#default_value' => $config->get('show_published_nodes'), '#description' => $this->t('Show Published Nodes.'), ]; $form['settings']['show_user_ip'] = [ '#type' => 'checkbox', '#title' => $this->t('Show User IP'), '#default_value' => $config->get('show_user_ip'), '#description' => $this->t('Show User IP.'), ]; $form['settings']['show_since_date'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Since Date'), '#default_value' => $config->get('show_since_date'), '#description' => $this->t('Show Since Date.'), ]; // Statistics settings. $form['statistics'] = [ '#type' => 'fieldset', '#title' => $this->t('Visitors statistics'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#description' => $this->t('Visitors statistics settings'), ]; $form['statistics']['exclude_user1'] = [ '#type' => 'checkbox', '#title' => $this->t('Exclude user1 from statistics'), '#default_value' => $config->get('exclude_user1'), '#description' => $this->t('Exclude hits of user1 from statistics.'), ]; $roles = []; foreach ($this->entityTypeManager->getStorage('user_role')->loadMultiple() as $name => $role) { $roles[$name] = $role->label(); } $form['statistics']['excluded_roles'] = [ $form['excluded_roles'] = [ '#type' => 'checkboxes', '#options' => $roles, '#title' => $this->t('Exclude users with roles from statistics'), Loading @@ -181,6 +101,13 @@ class Settings extends ConfigFormBase { '#description' => $this->t('Exclude hits of users with role(s) from statistics.'), ]; $form['exclude_user1'] = [ '#type' => 'checkbox', '#title' => $this->t('Exclude user1 from statistics'), '#default_value' => $config->get('exclude_user1'), '#description' => $this->t('Exclude hits of user1 from statistics.'), ]; $all_themes = $this->themeList->getList(); $default_theme = $system_config->get('default'); $admin_theme = $system_config->get('admin'); Loading @@ -199,7 +126,7 @@ class Settings extends ConfigFormBase { $themes_installed += array_map(function ($value) { return $value->info['name']; }, $list_themes); $form['statistics']['theme'] = [ $form['theme'] = [ '#type' => 'select', '#title' => $this->t('Set a theme for reports'), '#options' => $themes_installed, Loading @@ -207,7 +134,7 @@ class Settings extends ConfigFormBase { '#description' => $this->t('Select a theme for the Visitors reports.'), ]; $form['statistics']['items_per_page'] = [ $form['items_per_page'] = [ '#type' => 'select', '#title' => 'Items per page', '#default_value' => $config->get('items_per_page'), Loading @@ -226,7 +153,7 @@ class Settings extends ConfigFormBase { $this->t('The default maximum number of items to display per page.'), ]; $form['statistics']['flush_log_timer'] = [ $form['flush_log_timer'] = [ '#type' => 'select', '#title' => $this->t('Discard visitors logs older than'), '#default_value' => $config->get('flush_log_timer'), Loading @@ -253,26 +180,92 @@ class Settings extends ConfigFormBase { ), ]; // Chart settings. $form['chart_settings'] = [ '#type' => 'fieldset', '#title' => $this->t('Chart settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, $form['information'] = [ '#type' => 'vertical_tabs', '#default_tab' => 'edit-publication', ]; $form['block'] = [ '#type' => 'details', '#title' => $this->t('Default Block'), '#description' => $this->t('Default block settings'), '#group' => 'information', ]; $form['block']['show_total_visitors'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Total Visitors'), '#default_value' => $config->get('show_total_visitors'), '#description' => $this->t('Show Total Visitors.'), ]; $form['block']['show_unique_visitor'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Unique Visitors'), '#default_value' => $config->get('show_unique_visitor'), '#description' => $this->t('Show Unique Visitors based on their IP.'), ]; $form['block']['show_registered_users_count'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Registered Users Count'), '#default_value' => $config->get('show_registered_users_count'), '#description' => $this->t('Show Registered Users.'), ]; $form['block']['show_last_registered_user'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Last Registered User'), '#default_value' => $config->get('show_last_registered_user'), '#description' => $this->t('Show Last Registered User.'), ]; $form['block']['show_published_nodes'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Published Nodes'), '#default_value' => $config->get('show_published_nodes'), '#description' => $this->t('Show Published Nodes.'), ]; $form['block']['show_user_ip'] = [ '#type' => 'checkbox', '#title' => $this->t('Show User IP'), '#default_value' => $config->get('show_user_ip'), '#description' => $this->t('Show User IP.'), ]; $form['block']['show_since_date'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Since Date'), '#default_value' => $config->get('show_since_date'), '#description' => $this->t('Show Since Date.'), ]; $form['block']['start_count_total_visitors'] = [ '#type' => 'number', '#title' => $this->t('Total visitors start count'), '#default_value' => $config->get('start_count_total_visitors') ?? 0, '#description' => $this->t('Start the count of the total visitors at this number. Useful for including the known number of visitors in the past.'), ]; $form['charts'] = [ '#type' => 'details', '#title' => $this->t('Charts'), '#description' => $this->t('Visitors chart settings'), '#group' => 'information', ]; $form['chart_settings']['chart_width'] = [ '#type' => 'textfield', $form['charts']['chart_width'] = [ '#type' => 'number', '#title' => $this->t('Width'), '#default_value' => $config->get('chart_width'), '#default_value' => $config->get('chart_width') ?? 700, '#description' => $this->t('Chart width.'), ]; $form['chart_settings']['chart_height'] = [ '#type' => 'textfield', $form['charts']['chart_height'] = [ '#type' => 'number', '#title' => $this->t('Height'), '#default_value' => $config->get('chart_height'), '#default_value' => $config->get('chart_height') ?? 430, '#description' => $this->t('Chart height.'), ]; Loading src/Plugin/Block/VisitorsBlock.php +247 −72 Original line number Diff line number Diff line Loading @@ -3,6 +3,14 @@ namespace Drupal\visitors\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Render\RendererInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a 'Visitors' block. Loading @@ -13,142 +21,309 @@ use Drupal\Core\Block\BlockBase; * category = @Translation("Visitors") * ) */ class VisitorsBlock extends BlockBase { protected $config; protected $items; class VisitorsBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * The date service. * * @var \Drupal\Core\Datetime\DateFormatterInterface */ protected $dateFormatter; /** * Database Service Object. * * @var \Drupal\Core\Database\Connection */ protected $database; /** * The renderer service. * * @var \Drupal\Core\Render\RendererInterface */ protected $renderer; /** * The config factory. * * @var \Drupal\Core\Config\ConfigFactoryInterface */ protected $configFactory; /** * * @param \Drupal\Core\Database\Connection $database * The database service object. * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer service. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, RendererInterface $renderer, DateFormatterInterface $date_formatter, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->setConfiguration($configuration); $this->database = $database; $this->entityTypeManager = $entity_type_manager; $this->renderer = $renderer; $this->dateFormatter = $date_formatter; $this->configFactory = $config_factory; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('database'), $container->get('renderer'), $container->get('date.formatter'), $container->get('entity_type.manager'), $container->get('config.factory'), $container->get('request_stack') ); } /** * {@inheritdoc} */ public function build() { $this->config = \Drupal::config('visitors.config'); $this->items = []; $this->showTotalVisitors(); $this->showUniqueVisitors(); $this->showRegisteredUsersCount(); $this->showLastRegisteredUser(); $this->showPublishedNodes(); $this->showUserIp(); $this->showSinceDate(); return [ $items = []; $config = $this->getConfiguration(); if ($config['show_total_visitors']) { $items[] = $this->showTotalVisitors(); } if ($config['show_unique_visitor']) { $items[] = $this->showUniqueVisitors(); } if ($config['show_registered_users_count']) { $items[] = $this->showRegisteredUsersCount(); } if ($config['show_last_registered_user']) { $items[] = $this->showLastRegisteredUser(); } if ($config['show_published_nodes']) { $items[] = $this->showPublishedNodes(); } if ($config['show_user_ip']) { $items[] = $this->showUserIp(); } if ($config['show_since_date']) { $items[] = $this->showSinceDate(); } $build = [ 'visitors_info' => [ '#theme' => 'item_list', '#items' => $this->items, '#items' => $items, ], '#cache' => [ 'max-age' => 0, ], ]; return $build; } /** * {@inheritdoc} */ public function blockForm($form, FormStateInterface $form_state) { $form = parent::blockForm($form, $form_state); $default = $this->configFactory->get('visitors.config'); $default_config = [ 'show_total_visitors' => $default->get('show_total_visitors'), 'show_unique_visitor' => $default->get('show_unique_visitor'), 'show_registered_users_count' => $default->get('show_registered_users_count'), 'show_last_registered_user' => $default->get('show_last_registered_user'), 'show_published_nodes' => $default->get('show_published_nodes'), 'show_user_ip' => $default->get('show_user_ip'), 'show_since_date' => $default->get('show_since_date'), ]; $block_config = $this->getConfiguration(); $config = array_merge($default_config, $block_config); $form['show_total_visitors'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Total Visitors'), '#default_value' => $config['show_total_visitors'], '#description' => $this->t('Show Total Visitors.'), ]; $form['show_unique_visitor'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Unique Visitors'), '#default_value' => $config['show_unique_visitor'], '#description' => $this->t('Show Unique Visitors based on their IP.'), ]; $form['show_registered_users_count'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Registered Users Count'), '#default_value' => $config['show_registered_users_count'], '#description' => $this->t('Show Registered Users.'), ]; $form['show_last_registered_user'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Last Registered User'), '#default_value' => $config['show_last_registered_user'], '#description' => $this->t('Show Last Registered User.'), ]; $form['show_published_nodes'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Published Nodes'), '#default_value' => $config['show_published_nodes'], '#description' => $this->t('Show Published Nodes.'), ]; $form['show_user_ip'] = [ '#type' => 'checkbox', '#title' => $this->t('Show User IP'), '#default_value' => $config['show_user_ip'], '#description' => $this->t('Show User IP.'), ]; $form['show_since_date'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Since Date'), '#default_value' => $config['show_since_date'], '#description' => $this->t('Show Since Date.'), ]; return $form; } /** * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { $this->setConfigurationValue('show_total_visitors', $form_state->getValue('show_total_visitors')); $this->setConfigurationValue('show_unique_visitor', $form_state->getValue('show_unique_visitor')); $this->setConfigurationValue('show_registered_users_count', $form_state->getValue('show_registered_users_count')); $this->setConfigurationValue('show_last_registered_user', $form_state->getValue('show_last_registered_user')); $this->setConfigurationValue('show_published_nodes', $form_state->getValue('show_published_nodes')); $this->setConfigurationValue('show_user_ip', $form_state->getValue('show_user_ip')); $this->setConfigurationValue('show_since_date', $form_state->getValue('show_since_date')); } /** * Display total visitors count to visitors block. */ protected function showTotalVisitors() { if ($this->config->get('show_total_visitors')) { $query = \Drupal::database()->select('visitors'); $config = $this->configFactory->get('visitors.config'); $query = $this->database->select('visitors'); $query->addExpression('COUNT(*)'); $count = $query->execute()->fetchField() + $this->config->get('start_count_total_visitors'); $start_count_total_visitors = $config->get('start_count_total_visitors') ?? 0; $count = $query->execute()->fetchField() + $start_count_total_visitors; $this->items[] = $this->t('Total Visitors: %visitors', ['%visitors' => $count]); } $item = $this->t('Total Visitors: %visitors', ['%visitors' => $count]); return $item; } /** * Display unique visitors count to visitors block. */ protected function showUniqueVisitors() { if ($this->config->get('show_unique_visitor')) { $query = \Drupal::database()->select('visitors'); $query = $this->database->select('visitors'); $query->addExpression('COUNT(DISTINCT visitors_ip)'); $unique_visitors = $query->execute()->fetchField(); $this->items[] = $this->t('Unique Visitors: %unique_visitors', ['%unique_visitors' => $unique_visitors]); } $item = $this->t('Unique Visitors: %unique_visitors', ['%unique_visitors' => $unique_visitors]); return $item; } /** * Display registered users count to visitors block. */ protected function showRegisteredUsersCount() { if ($this->config->get('show_registered_users_count')) { $query = \Drupal::database()->select('users'); $query = $this->database->select('users'); $query->addExpression('COUNT(*)'); $query->condition('uid', '0', '>'); $registered_users_count = $query->execute()->fetchField(); $this->items[] = $this->t('Registered Users: %registered_users_count', ['%registered_users_count' => $registered_users_count]); } $item = $this->t('Registered Users: %registered_users_count', ['%registered_users_count' => $registered_users_count]); return $item; } /** * Display last registered user to visitors block. */ protected function showLastRegisteredUser() { if ($this->config->get('show_last_registered_user')) { $last_user_uid = \Drupal::database()->select('users', 'u') $last_user_uid = $this->database->select('users', 'u') ->fields('u', ['uid']) ->orderBy('uid', 'DESC') ->range(0, 1) ->execute() ->fetchField(); $user = \Drupal::entityTypeManager()->getStorage('user')->load($last_user_uid); $user = $this->entityTypeManager->getStorage('user')->load($last_user_uid); $username = [ '#theme' => 'username', '#account' => $user, ]; $this->items[] = $this->t('Last Registered User: @last_user', ['@last_user' => \Drupal::service('renderer')->render($username)]); } $item = $this->t('Last Registered User: @last_user', ['@last_user' => $this->renderer->render($username)]); return $item; } /** * Display published nodes count to visitors block. */ protected function showPublishedNodes() { if ($this->config->get('show_published_nodes')) { $query = \Drupal::database()->select('node', 'n'); $query = $this->database->select('node', 'n'); $query->innerJoin('node_field_data', 'nfd', 'n.nid = nfd.nid'); $query->addExpression('COUNT(*)'); $query->condition('nfd.status', '1', '='); $nodes = $query->execute()->fetchField(); $this->items[] = $this->t('Published Nodes: %nodes', ['%nodes' => $nodes]); } $item = $this->t('Published Nodes: %nodes', ['%nodes' => $nodes]); return $item; } /** * Display user ip to visitors block. */ protected function showUserIp() { if ($this->config->get('show_user_ip')) { $this->items[] = $this->t('Your IP: %user_ip', ['%user_ip' => \Drupal::request()->getClientIp()]); } $item = $this->t('Your IP: %user_ip', ['%user_ip' => \Drupal::request()->getClientIp()]); return $item; } /** * Display the start date statistics to visitors block. */ protected function showSinceDate() { if ($this->config->get('show_since_date')) { $query = \Drupal::database()->select('visitors'); $query = $this->database->select('visitors'); $query->addExpression('MIN(visitors_date_time)'); $since_date = $query->execute()->fetchField(); $this->items[] = $this->t('Since: %since_date', [ '%since_date' => \Drupal::service('date.formatter')->format($since_date, 'short'), $item = $this->t('Since: %since_date', [ '%since_date' => $this->dateFormatter->format($since_date, 'short'), ]); } return $item; } } Loading
src/Form/Settings.php +89 −96 Original line number Diff line number Diff line Loading @@ -88,92 +88,12 @@ class Settings extends ConfigFormBase { $system_config = $this->config('system.theme'); $form = parent::buildForm($form, $form_state); $form['settings'] = [ '#type' => 'fieldset', '#weight' => -30, '#title' => $this->t('Visitors block'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#description' => $this->t('Visitors block settings'), ]; $form['settings']['show_total_visitors'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Total Visitors'), '#default_value' => $config->get('show_total_visitors'), '#description' => $this->t('Show Total Visitors.'), ]; $form['settings']['start_count_total_visitors'] = [ '#type' => 'textfield', '#title' => $this->t('Total visitors start count'), '#default_value' => $config->get('start_count_total_visitors'), '#description' => $this->t('Start the count of the total visitors at this number. Useful for including the known number of visitors in the past.'), ]; $form['settings']['show_unique_visitor'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Unique Visitors'), '#default_value' => $config->get('show_unique_visitor'), '#description' => $this->t('Show Unique Visitors based on their IP.'), ]; $form['settings']['show_registered_users_count'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Registered Users Count'), '#default_value' => $config->get('show_registered_users_count'), '#description' => $this->t('Show Registered Users.'), ]; $form['settings']['show_last_registered_user'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Last Registered User'), '#default_value' => $config->get('show_last_registered_user'), '#description' => $this->t('Show Last Registered User.'), ]; $form['settings']['show_published_nodes'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Published Nodes'), '#default_value' => $config->get('show_published_nodes'), '#description' => $this->t('Show Published Nodes.'), ]; $form['settings']['show_user_ip'] = [ '#type' => 'checkbox', '#title' => $this->t('Show User IP'), '#default_value' => $config->get('show_user_ip'), '#description' => $this->t('Show User IP.'), ]; $form['settings']['show_since_date'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Since Date'), '#default_value' => $config->get('show_since_date'), '#description' => $this->t('Show Since Date.'), ]; // Statistics settings. $form['statistics'] = [ '#type' => 'fieldset', '#title' => $this->t('Visitors statistics'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#description' => $this->t('Visitors statistics settings'), ]; $form['statistics']['exclude_user1'] = [ '#type' => 'checkbox', '#title' => $this->t('Exclude user1 from statistics'), '#default_value' => $config->get('exclude_user1'), '#description' => $this->t('Exclude hits of user1 from statistics.'), ]; $roles = []; foreach ($this->entityTypeManager->getStorage('user_role')->loadMultiple() as $name => $role) { $roles[$name] = $role->label(); } $form['statistics']['excluded_roles'] = [ $form['excluded_roles'] = [ '#type' => 'checkboxes', '#options' => $roles, '#title' => $this->t('Exclude users with roles from statistics'), Loading @@ -181,6 +101,13 @@ class Settings extends ConfigFormBase { '#description' => $this->t('Exclude hits of users with role(s) from statistics.'), ]; $form['exclude_user1'] = [ '#type' => 'checkbox', '#title' => $this->t('Exclude user1 from statistics'), '#default_value' => $config->get('exclude_user1'), '#description' => $this->t('Exclude hits of user1 from statistics.'), ]; $all_themes = $this->themeList->getList(); $default_theme = $system_config->get('default'); $admin_theme = $system_config->get('admin'); Loading @@ -199,7 +126,7 @@ class Settings extends ConfigFormBase { $themes_installed += array_map(function ($value) { return $value->info['name']; }, $list_themes); $form['statistics']['theme'] = [ $form['theme'] = [ '#type' => 'select', '#title' => $this->t('Set a theme for reports'), '#options' => $themes_installed, Loading @@ -207,7 +134,7 @@ class Settings extends ConfigFormBase { '#description' => $this->t('Select a theme for the Visitors reports.'), ]; $form['statistics']['items_per_page'] = [ $form['items_per_page'] = [ '#type' => 'select', '#title' => 'Items per page', '#default_value' => $config->get('items_per_page'), Loading @@ -226,7 +153,7 @@ class Settings extends ConfigFormBase { $this->t('The default maximum number of items to display per page.'), ]; $form['statistics']['flush_log_timer'] = [ $form['flush_log_timer'] = [ '#type' => 'select', '#title' => $this->t('Discard visitors logs older than'), '#default_value' => $config->get('flush_log_timer'), Loading @@ -253,26 +180,92 @@ class Settings extends ConfigFormBase { ), ]; // Chart settings. $form['chart_settings'] = [ '#type' => 'fieldset', '#title' => $this->t('Chart settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, $form['information'] = [ '#type' => 'vertical_tabs', '#default_tab' => 'edit-publication', ]; $form['block'] = [ '#type' => 'details', '#title' => $this->t('Default Block'), '#description' => $this->t('Default block settings'), '#group' => 'information', ]; $form['block']['show_total_visitors'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Total Visitors'), '#default_value' => $config->get('show_total_visitors'), '#description' => $this->t('Show Total Visitors.'), ]; $form['block']['show_unique_visitor'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Unique Visitors'), '#default_value' => $config->get('show_unique_visitor'), '#description' => $this->t('Show Unique Visitors based on their IP.'), ]; $form['block']['show_registered_users_count'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Registered Users Count'), '#default_value' => $config->get('show_registered_users_count'), '#description' => $this->t('Show Registered Users.'), ]; $form['block']['show_last_registered_user'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Last Registered User'), '#default_value' => $config->get('show_last_registered_user'), '#description' => $this->t('Show Last Registered User.'), ]; $form['block']['show_published_nodes'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Published Nodes'), '#default_value' => $config->get('show_published_nodes'), '#description' => $this->t('Show Published Nodes.'), ]; $form['block']['show_user_ip'] = [ '#type' => 'checkbox', '#title' => $this->t('Show User IP'), '#default_value' => $config->get('show_user_ip'), '#description' => $this->t('Show User IP.'), ]; $form['block']['show_since_date'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Since Date'), '#default_value' => $config->get('show_since_date'), '#description' => $this->t('Show Since Date.'), ]; $form['block']['start_count_total_visitors'] = [ '#type' => 'number', '#title' => $this->t('Total visitors start count'), '#default_value' => $config->get('start_count_total_visitors') ?? 0, '#description' => $this->t('Start the count of the total visitors at this number. Useful for including the known number of visitors in the past.'), ]; $form['charts'] = [ '#type' => 'details', '#title' => $this->t('Charts'), '#description' => $this->t('Visitors chart settings'), '#group' => 'information', ]; $form['chart_settings']['chart_width'] = [ '#type' => 'textfield', $form['charts']['chart_width'] = [ '#type' => 'number', '#title' => $this->t('Width'), '#default_value' => $config->get('chart_width'), '#default_value' => $config->get('chart_width') ?? 700, '#description' => $this->t('Chart width.'), ]; $form['chart_settings']['chart_height'] = [ '#type' => 'textfield', $form['charts']['chart_height'] = [ '#type' => 'number', '#title' => $this->t('Height'), '#default_value' => $config->get('chart_height'), '#default_value' => $config->get('chart_height') ?? 430, '#description' => $this->t('Chart height.'), ]; Loading
src/Plugin/Block/VisitorsBlock.php +247 −72 Original line number Diff line number Diff line Loading @@ -3,6 +3,14 @@ namespace Drupal\visitors\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Render\RendererInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a 'Visitors' block. Loading @@ -13,142 +21,309 @@ use Drupal\Core\Block\BlockBase; * category = @Translation("Visitors") * ) */ class VisitorsBlock extends BlockBase { protected $config; protected $items; class VisitorsBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * The date service. * * @var \Drupal\Core\Datetime\DateFormatterInterface */ protected $dateFormatter; /** * Database Service Object. * * @var \Drupal\Core\Database\Connection */ protected $database; /** * The renderer service. * * @var \Drupal\Core\Render\RendererInterface */ protected $renderer; /** * The config factory. * * @var \Drupal\Core\Config\ConfigFactoryInterface */ protected $configFactory; /** * * @param \Drupal\Core\Database\Connection $database * The database service object. * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer service. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, RendererInterface $renderer, DateFormatterInterface $date_formatter, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->setConfiguration($configuration); $this->database = $database; $this->entityTypeManager = $entity_type_manager; $this->renderer = $renderer; $this->dateFormatter = $date_formatter; $this->configFactory = $config_factory; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('database'), $container->get('renderer'), $container->get('date.formatter'), $container->get('entity_type.manager'), $container->get('config.factory'), $container->get('request_stack') ); } /** * {@inheritdoc} */ public function build() { $this->config = \Drupal::config('visitors.config'); $this->items = []; $this->showTotalVisitors(); $this->showUniqueVisitors(); $this->showRegisteredUsersCount(); $this->showLastRegisteredUser(); $this->showPublishedNodes(); $this->showUserIp(); $this->showSinceDate(); return [ $items = []; $config = $this->getConfiguration(); if ($config['show_total_visitors']) { $items[] = $this->showTotalVisitors(); } if ($config['show_unique_visitor']) { $items[] = $this->showUniqueVisitors(); } if ($config['show_registered_users_count']) { $items[] = $this->showRegisteredUsersCount(); } if ($config['show_last_registered_user']) { $items[] = $this->showLastRegisteredUser(); } if ($config['show_published_nodes']) { $items[] = $this->showPublishedNodes(); } if ($config['show_user_ip']) { $items[] = $this->showUserIp(); } if ($config['show_since_date']) { $items[] = $this->showSinceDate(); } $build = [ 'visitors_info' => [ '#theme' => 'item_list', '#items' => $this->items, '#items' => $items, ], '#cache' => [ 'max-age' => 0, ], ]; return $build; } /** * {@inheritdoc} */ public function blockForm($form, FormStateInterface $form_state) { $form = parent::blockForm($form, $form_state); $default = $this->configFactory->get('visitors.config'); $default_config = [ 'show_total_visitors' => $default->get('show_total_visitors'), 'show_unique_visitor' => $default->get('show_unique_visitor'), 'show_registered_users_count' => $default->get('show_registered_users_count'), 'show_last_registered_user' => $default->get('show_last_registered_user'), 'show_published_nodes' => $default->get('show_published_nodes'), 'show_user_ip' => $default->get('show_user_ip'), 'show_since_date' => $default->get('show_since_date'), ]; $block_config = $this->getConfiguration(); $config = array_merge($default_config, $block_config); $form['show_total_visitors'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Total Visitors'), '#default_value' => $config['show_total_visitors'], '#description' => $this->t('Show Total Visitors.'), ]; $form['show_unique_visitor'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Unique Visitors'), '#default_value' => $config['show_unique_visitor'], '#description' => $this->t('Show Unique Visitors based on their IP.'), ]; $form['show_registered_users_count'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Registered Users Count'), '#default_value' => $config['show_registered_users_count'], '#description' => $this->t('Show Registered Users.'), ]; $form['show_last_registered_user'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Last Registered User'), '#default_value' => $config['show_last_registered_user'], '#description' => $this->t('Show Last Registered User.'), ]; $form['show_published_nodes'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Published Nodes'), '#default_value' => $config['show_published_nodes'], '#description' => $this->t('Show Published Nodes.'), ]; $form['show_user_ip'] = [ '#type' => 'checkbox', '#title' => $this->t('Show User IP'), '#default_value' => $config['show_user_ip'], '#description' => $this->t('Show User IP.'), ]; $form['show_since_date'] = [ '#type' => 'checkbox', '#title' => $this->t('Show Since Date'), '#default_value' => $config['show_since_date'], '#description' => $this->t('Show Since Date.'), ]; return $form; } /** * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { $this->setConfigurationValue('show_total_visitors', $form_state->getValue('show_total_visitors')); $this->setConfigurationValue('show_unique_visitor', $form_state->getValue('show_unique_visitor')); $this->setConfigurationValue('show_registered_users_count', $form_state->getValue('show_registered_users_count')); $this->setConfigurationValue('show_last_registered_user', $form_state->getValue('show_last_registered_user')); $this->setConfigurationValue('show_published_nodes', $form_state->getValue('show_published_nodes')); $this->setConfigurationValue('show_user_ip', $form_state->getValue('show_user_ip')); $this->setConfigurationValue('show_since_date', $form_state->getValue('show_since_date')); } /** * Display total visitors count to visitors block. */ protected function showTotalVisitors() { if ($this->config->get('show_total_visitors')) { $query = \Drupal::database()->select('visitors'); $config = $this->configFactory->get('visitors.config'); $query = $this->database->select('visitors'); $query->addExpression('COUNT(*)'); $count = $query->execute()->fetchField() + $this->config->get('start_count_total_visitors'); $start_count_total_visitors = $config->get('start_count_total_visitors') ?? 0; $count = $query->execute()->fetchField() + $start_count_total_visitors; $this->items[] = $this->t('Total Visitors: %visitors', ['%visitors' => $count]); } $item = $this->t('Total Visitors: %visitors', ['%visitors' => $count]); return $item; } /** * Display unique visitors count to visitors block. */ protected function showUniqueVisitors() { if ($this->config->get('show_unique_visitor')) { $query = \Drupal::database()->select('visitors'); $query = $this->database->select('visitors'); $query->addExpression('COUNT(DISTINCT visitors_ip)'); $unique_visitors = $query->execute()->fetchField(); $this->items[] = $this->t('Unique Visitors: %unique_visitors', ['%unique_visitors' => $unique_visitors]); } $item = $this->t('Unique Visitors: %unique_visitors', ['%unique_visitors' => $unique_visitors]); return $item; } /** * Display registered users count to visitors block. */ protected function showRegisteredUsersCount() { if ($this->config->get('show_registered_users_count')) { $query = \Drupal::database()->select('users'); $query = $this->database->select('users'); $query->addExpression('COUNT(*)'); $query->condition('uid', '0', '>'); $registered_users_count = $query->execute()->fetchField(); $this->items[] = $this->t('Registered Users: %registered_users_count', ['%registered_users_count' => $registered_users_count]); } $item = $this->t('Registered Users: %registered_users_count', ['%registered_users_count' => $registered_users_count]); return $item; } /** * Display last registered user to visitors block. */ protected function showLastRegisteredUser() { if ($this->config->get('show_last_registered_user')) { $last_user_uid = \Drupal::database()->select('users', 'u') $last_user_uid = $this->database->select('users', 'u') ->fields('u', ['uid']) ->orderBy('uid', 'DESC') ->range(0, 1) ->execute() ->fetchField(); $user = \Drupal::entityTypeManager()->getStorage('user')->load($last_user_uid); $user = $this->entityTypeManager->getStorage('user')->load($last_user_uid); $username = [ '#theme' => 'username', '#account' => $user, ]; $this->items[] = $this->t('Last Registered User: @last_user', ['@last_user' => \Drupal::service('renderer')->render($username)]); } $item = $this->t('Last Registered User: @last_user', ['@last_user' => $this->renderer->render($username)]); return $item; } /** * Display published nodes count to visitors block. */ protected function showPublishedNodes() { if ($this->config->get('show_published_nodes')) { $query = \Drupal::database()->select('node', 'n'); $query = $this->database->select('node', 'n'); $query->innerJoin('node_field_data', 'nfd', 'n.nid = nfd.nid'); $query->addExpression('COUNT(*)'); $query->condition('nfd.status', '1', '='); $nodes = $query->execute()->fetchField(); $this->items[] = $this->t('Published Nodes: %nodes', ['%nodes' => $nodes]); } $item = $this->t('Published Nodes: %nodes', ['%nodes' => $nodes]); return $item; } /** * Display user ip to visitors block. */ protected function showUserIp() { if ($this->config->get('show_user_ip')) { $this->items[] = $this->t('Your IP: %user_ip', ['%user_ip' => \Drupal::request()->getClientIp()]); } $item = $this->t('Your IP: %user_ip', ['%user_ip' => \Drupal::request()->getClientIp()]); return $item; } /** * Display the start date statistics to visitors block. */ protected function showSinceDate() { if ($this->config->get('show_since_date')) { $query = \Drupal::database()->select('visitors'); $query = $this->database->select('visitors'); $query->addExpression('MIN(visitors_date_time)'); $since_date = $query->execute()->fetchField(); $this->items[] = $this->t('Since: %since_date', [ '%since_date' => \Drupal::service('date.formatter')->format($since_date, 'short'), $item = $this->t('Since: %since_date', [ '%since_date' => $this->dateFormatter->format($since_date, 'short'), ]); } return $item; } }