View source
<?php
namespace Drupal\visitors\Controller\Report;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\Date;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DaysOfMonth extends ControllerBase {
protected $date;
protected $formBuilder;
public static function create(ContainerInterface $container) {
return new static($container
->get('date.formatter'), $container
->get('form_builder'));
}
public function __construct(DateFormatterInterface $date_formatter, FormBuilderInterface $form_builder) {
$this->date = $date_formatter;
$this->formBuilder = $form_builder;
}
public function display() {
$config = \Drupal::config('visitors.config');
$form = $this->formBuilder
->getForm('Drupal\\visitors\\Form\\DateFilter');
$header = $this
->_getHeader();
$results = $this
->_getData(NULL);
$tmp_rows = array();
$y = array();
for ($i = 1; $i <= 31; $i++) {
$y[$i] = 0;
}
foreach ($results as $data) {
$y[$data[1]] = $data[2];
}
return array(
'visitors_date_filter_form' => $form,
'visitors_jqplot' => array(
'#theme' => 'visitors_jqplot',
'#path' => drupal_get_path('module', 'visitors'),
'#x' => implode(', ', range(1, 31)),
'#y' => implode(', ', $y),
'#width' => $config
->get('chart_width'),
'#height' => $config
->get('chart_height'),
),
'visitors_table' => array(
'#type' => 'table',
'#header' => $header,
'#rows' => $this
->_getData($header),
),
);
}
protected function _getHeader() {
return array(
'#' => array(
'data' => t('#'),
),
'day' => array(
'data' => t('Day'),
'field' => 'day',
'specifier' => 'day',
'class' => array(
RESPONSIVE_PRIORITY_LOW,
),
'sort' => 'asc',
),
'count' => array(
'data' => t('Pages'),
'field' => 'count',
'specifier' => 'count',
'class' => array(
RESPONSIVE_PRIORITY_LOW,
),
),
);
}
protected function _getData($header) {
$query = \Drupal::database()
->select('visitors', 'v');
$query
->addExpression('COUNT(*)', 'count');
$query
->addExpression(visitors_date_format_sql('visitors_date_time', '%d'), 'day');
$query
->groupBy('day');
visitors_date_filter_sql_condition($query);
if (!is_null($header)) {
$query
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
->orderByHeader($header);
}
$results = $query
->execute();
$rows = array();
$i = 0;
foreach ($results as $data) {
$rows[] = array(
++$i,
(int) $data->day,
$data->count,
);
}
return $rows;
}
}