work_time-1.0.x-dev/src/Plugin/views/style/WorkTime.php
src/Plugin/views/style/WorkTime.php
<?php
namespace Drupal\work_time\Plugin\views\style;
use Drupal\core\form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\style\Table;
/**
* Style plugin to render a list of work time in months.
*
* @ingroup views_style_plugins
*
* @ViewsStyle(
* id = "worktime",
* title = @Translation("Work time"),
* help = @Translation("Render a list of worktime."),
* theme = "views_view_work_time",
* display_types = { "normal" }
* )
*/
class WorkTime extends Table {
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['total'] = ['default' => 'time_total'];
$options['group'] = ['default' => 'reference_id'];
$options['filter_time'] = ['default' => 'month'];
$options['column_aggregation'] = [
'totals_row_position' => ['default' => [1 => 0, 2 => 2, 3 => 0]],
'totals_row_class' => ['default' => ''],
'default' => FALSE,
];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$fields = $this->displayHandler->getHandlers('field');
$labels = $this->displayHandler->getFieldLabels();
$field_labels = [];
foreach ($fields as $field_name => $field) {
$field_labels[$field_name] = $labels[$field_name];
}
$form['group'] = [
'#type' => 'select',
'#title' => $this->t('Group'),
'#options' => $field_labels,
'#required' => TRUE,
'#description' => $this->t('Group field.'),
'#default_value' => $this->options['group'] ?? '',
];
$form['total'] = [
'#type' => 'select',
'#title' => $this->t('Total time field'),
'#description' => $this->t('Select a time total field'),
'#options' => $field_labels,
'#required' => TRUE,
'#default_value' => $this->options['total'] ?? '',
];
$form['filter_time'] = [
'#type' => 'select',
'#title' => $this->t('Filter by'),
'#description' => $this->t('Filter time total, use query example for month ?date=@month or for week ?date=@week or for year ?date=@year', [
'@month' => date('Y-m'),
'@week' => date('Y-\WW'),
'@year' => date('Y'),
]),
'#options' => [
'month' => $this->t('Month'),
'week' => $this->t('Week'),
'year' => $this->t('Year'),
],
'#required' => TRUE,
'#default_value' => $this->options['filter_time'] ?? '',
];
$form['column_aggregation'] = [
'#type' => 'details',
'#title' => $this->t('Total options'),
];
$form['column_aggregation']['totals_row_class'] = [
'#title' => $this->t('Total row class'),
'#type' => 'textfield',
'#description' => $this->t('The CSS class to provide on the row containing the column aggregations.'),
'#default_value' => $this->options['column_aggregation']['totals_row_class'] ?? '',
'#weight' => 3,
];
}
/**
* {@inheritdoc}
*/
public function query() {
// Override the query generated by the view.
$current_user = \Drupal::currentUser();
$holiday = \Drupal::service('work_time.holiday');
$roles = $current_user->getRoles();
$uid = $current_user->id();
if (!in_array('administrator', $roles) || !$current_user->hasPermission('administer work time')) {
$this->view->query->addWhere(0, 'uid', $uid, '=');
}
$this->view->query->addWhere(0, 'stopped', NULL, 'IS NOT');
$filter = \Drupal::request()->query->get('date');
if (!empty($this->options["filter_time"])) {
switch ($this->options["filter_time"]) {
case 'month':
if (empty($filter)) {
$filter = date('Y-m');
}
$timestamp = strtotime($filter . '-01');
$start = strtotime('first day of this month', $timestamp);
$end = strtotime('last day of this month', $timestamp) + 24 * 3600 - 1;
break;
case 'week':
if (empty($filter)) {
$filter = date('Y-\WW');
}
$timestamp = strtotime($filter);
$start = strtotime('this monday', $timestamp);
$end = strtotime('this sunday', $timestamp);
break;
case 'year':
$month = date('m');
if (empty($filter)) {
$filter = date('Y');
}
$year = $filter;
if ($year < date('Y')) {
$month = 12;
}
$timestamp = strtotime("$year-$month-01");
$start = strtotime("$year-01-01");
$end = strtotime('last day of this month', $timestamp) + 24 * 3600 - 1;
break;
}
$this->view->query->addWhere(0, 'created', [$start, $end], 'BETWEEN');
}
$this->view->element['#attached']['library'][] = 'work_time/bootstrap_table';
$displayObj = $this->view->getDisplay();
$param = [
'mode_display' => $this->options["group"],
'mode_time' => $this->options['filter_time'],
'date' => $filter,
];
$holidays = $holiday->getHolidays('', substr($filter, 0, 4));
$url = Url::fromRoute('work_time.ajax_timegeneral', $param)->toString();
$this->view->element['#attached']['drupalSettings']['worktime'] = [
'url' => $url,
'options' => $this->options,
'filter' => $filter,
'view_id' => $this->view->id(),
'display_id' => $displayObj->display['id'],
'holidays' => $holidays,
];
// Call the parent render() method to generate the table.
parent::query();
}
/**
* {@inheritdoc}
*/
public function preRender($results) {
parent::preRender($results);
if (empty($this->view->result)) {
return;
}
if (!empty($this->view->total_rows)) {
$group = $this->options["group"];
$grouping = $this->options["grouping"];
$time_total = $this->options["total"];
if (!empty($grouping)) {
$grouping = $grouping[0]['field'];
}
$checked = [];
foreach ($results as $index => $result) {
$entity = $result->_entity;
$time = $entity->get($time_total)->value;
$groupValue = $entity->get($group)->getString();
if (!empty($grouping)) {
$groupingValue = $entity->get($grouping)->getString();
$groupValue .= $groupingValue;
}
if (empty($checked[$groupValue])) {
$checked[$groupValue] = [
'id' => $index,
'total' => [$time],
];
}
else {
$checked[$groupValue]['total'][] = $time;
$results[$checked[$groupValue]['id']]->_entity->set('time_total', array_sum($checked[$groupValue]['total']));
unset($results[$index]);
unset($this->view->result[$index]);
}
}
$this->renderFields($results);
}
}
}
