work_time-1.0.x-dev/work_time.views.theme.inc
work_time.views.theme.inc
<?php
/**
* @file
* Theme for worktime views.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\Core\Utility\TableSort;
use Drupal\Core\Template\Attribute;
/**
* Prepares variables for views table templates.
*
* Default template: views-view-work-time.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_view_work_time(array &$variables) {
$view = $variables['view'];
// We need the raw data for this grouping, which is passed in as
// $variables['rows']. However, the template also needs to use for
// the rendered fields. We therefore swap the raw data out to a new variable
// and reset $variables['rows'] so that it can get rebuilt.
// Store rows so that they may be used by further preprocess functions.
$result = $variables['result'] = $variables['rows'];
$variables['rows'] = [];
$variables['header'] = [];
$options = $view->style_plugin->options;
$handler = $view->style_plugin;
$fields = &$view->field;
$columns = $handler->sanitizeColumns($options['columns'], $fields);
$active = !empty($handler->active) ? $handler->active : '';
$order = !empty($handler->order) ? $handler->order : 'asc';
// A boolean variable which stores whether the table has a responsive class.
$responsive = FALSE;
// For the actual site we want to not render full URLs, because this would
// make pagers cacheable per URL, which is problematic in blocks, for example.
// For the actual live preview though the javascript relies on properly
// working URLs.
$route_name = !empty($view->live_preview) ? '<current>' : '<none>';
$query = TableSort::getQueryParameters(\Drupal::request());
if (isset($view->exposed_raw_input)) {
$query += $view->exposed_raw_input;
}
// A boolean to store whether the table's header has any labels.
$has_header_labels = FALSE;
foreach ($columns as $field => $column) {
// Create a second variable, so we can easily find what fields we have and
// what the CSS classes should be.
$variables['fields'][$field] = Html::cleanCssIdentifier($field);
if ($active == $field) {
$variables['fields'][$field] .= ' is-active';
}
// Render the header labels.
if ($field == $column && empty($fields[$field]->options['exclude'])) {
$label = !empty($fields[$field]) ? $fields[$field]->label() : '';
if (empty($options['info'][$field]['sortable']) || !$fields[$field]->clickSortable()) {
$variables['header'][$field]['content'] = $label;
}
else {
$initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
if ($active == $field) {
$initial = ($order == 'asc') ? 'desc' : 'asc';
}
$title = t('sort by @s', ['@s' => $label]);
if ($active == $field) {
$variables['header'][$field]['sort_indicator'] = [
'#theme' => 'tablesort_indicator',
'#style' => $initial,
];
}
$query['order'] = $field;
$query['sort'] = $initial;
$link_options = [
'query' => $query,
];
$url = new Url($route_name, [], $link_options);
$variables['header'][$field]['url'] = $url->toString();
$variables['header'][$field]['content'] = $label;
$variables['header'][$field]['title'] = $title;
}
$variables['header'][$field]['default_classes'] = $fields[$field]->options['element_default_classes'];
// Set up the header label class.
$variables['header'][$field]['attributes'] = [];
$class = $fields[$field]->elementLabelClasses(0);
if ($class) {
$variables['header'][$field]['attributes']['class'][] = $class;
}
// Add responsive header classes.
if (!empty($options['info'][$field]['responsive'])) {
$variables['header'][$field]['attributes']['class'][] = $options['info'][$field]['responsive'];
$responsive = TRUE;
}
// Add a CSS align class to each field if one was set.
if (!empty($options['info'][$field]['align'])) {
$variables['header'][$field]['attributes']['class'][] = Html::cleanCssIdentifier($options['info'][$field]['align']);
}
// Add a header label wrapper if one was selected.
if ($variables['header'][$field]['content']) {
$element_label_type = $fields[$field]->elementLabelType(TRUE, TRUE);
if ($element_label_type) {
$variables['header'][$field]['wrapper_element'] = $element_label_type;
}
// Improves accessibility of complex tables.
$variables['header'][$field]['attributes']['id'] = Html::getUniqueId('view-' . $field . '-table-column');
}
// Check if header label is not empty.
if (!empty($variables['header'][$field]['content'])) {
$has_header_labels = TRUE;
}
$variables['header'][$field]['attributes'] = new Attribute($variables['header'][$field]['attributes']);
}
// Add a CSS align class to each field if one was set.
if (!empty($options['info'][$field]['align'])) {
$variables['fields'][$field] .= ' ' . Html::cleanCssIdentifier($options['info'][$field]['align']);
}
// Render each field into its appropriate column.
foreach ($result as $num => $row) {
// Skip building the attributes and content if the field is to be excluded
// from the display.
if (!empty($fields[$field]->options['exclude'])) {
continue;
}
// Reference to the column in the loop to make the code easier to read.
$column_reference =& $variables['rows'][$num]['columns'][$column];
$column_reference['default_classes'] = $fields[$field]->options['element_default_classes'];
// Set the field key to the column, so it can be used for adding classes
// in a template.
$column_reference['fields'][] = $variables['fields'][$field];
// Add field classes.
if (!isset($column_reference['attributes'])) {
$column_reference['attributes'] = [];
}
if ($classes = $fields[$field]->elementClasses($num)) {
$column_reference['attributes']['class'][] = $classes;
}
// Add responsive header classes.
if (!empty($options['info'][$field]['responsive'])) {
$column_reference['attributes']['class'][] = $options['info'][$field]['responsive'];
}
// Improves accessibility of complex tables.
if (isset($variables['header'][$field]['attributes']['id'])) {
$column_reference['attributes']['headers'] = [$variables['header'][$field]['attributes']['id']];
}
if (!empty($fields[$field])) {
$field_output = $handler->getField($num, $field);
$column_reference['wrapper_element'] = $fields[$field]->elementType(TRUE, TRUE);
if (!isset($column_reference['content'])) {
$column_reference['content'] = [];
}
// Only bother with separators and stuff if the field shows up.
// Place the field into the column, along with an optional separator.
if (trim($field_output) != '') {
if (!empty($column_reference['content']) && !empty($options['info'][$column]['separator'])) {
$column_reference['content'][] = [
'separator' => ['#markup' => $options['info'][$column]['separator']],
'field_output' => ['#markup' => $field_output],
];
}
else {
$column_reference['content'][] = [
'field_output' => ['#markup' => $field_output],
];
}
}
}
$column_reference['attributes'] = new Attribute($column_reference['attributes']);
}
// Remove columns if the "empty_column" option is checked and the
// field is empty.
if (!empty($options['info'][$field]['empty_column'])) {
$empty = TRUE;
foreach ($variables['rows'] as $columns) {
$empty &= empty($columns['columns'][$column]['content']);
}
if ($empty) {
foreach ($variables['rows'] as &$column_items) {
unset($column_items['columns'][$column]);
}
unset($variables['header'][$column]);
}
}
}
// Hide table header if all labels are empty.
if (!$has_header_labels) {
$variables['header'] = [];
}
foreach ($variables['rows'] as $num => $row) {
$wt_entity = $result[$num]->_entity;
$variables['rows'][$num]['attributes'] = [
'data-uid-reference' => implode('-', [
$wt_entity->get('uid')->getString(),
$wt_entity->get('reference_id')->getString(),
]),
];
if ($row_class = $handler->getRowClass($num)) {
$variables['rows'][$num]['attributes']['class'][] = $row_class;
}
$variables['rows'][$num]['attributes'] = new Attribute($variables['rows'][$num]['attributes']);
}
if (empty($variables['rows']) && !empty($options['empty_table'])) {
$build = $view->display_handler->renderArea('empty');
$variables['rows'][0]['columns'][0]['content'][0]['field_output'] = $build;
$variables['rows'][0]['attributes'] = new Attribute(['class' => ['odd']]);
// Calculate the amounts of rows with output.
$variables['rows'][0]['columns'][0]['attributes'] = new Attribute([
'colspan' => count($variables['header']),
'class' => ['views-empty'],
]);
}
$variables['sticky'] = FALSE;
if (!empty($options['sticky'])) {
$variables['view']->element['#attached']['library'][] = 'core/drupal.tableheader';
$variables['sticky'] = TRUE;
}
// Add the caption to the list if set.
if (!empty($handler->options['caption'])) {
$variables['caption'] = ['#markup' => $handler->options['caption']];
$variables['caption_needed'] = TRUE;
}
elseif (!empty($variables['title'])) {
$variables['caption'] = ['#markup' => $variables['title']];
$variables['caption_needed'] = TRUE;
}
else {
$variables['caption'] = '';
$variables['caption_needed'] = FALSE;
}
// For backwards compatibility, initialize the 'summary' and 'description'
// variables, although core templates now all use 'summary_element' instead.
$variables['summary'] = $handler->options['summary'];
$variables['description'] = $handler->options['description'];
if (!empty($handler->options['summary']) || !empty($handler->options['description'])) {
$variables['summary_element'] = [
'#type' => 'details',
'#title' => $handler->options['summary'],
// To ensure that the description is properly escaped during rendering,
// use an 'inline_template' to let Twig do its magic, instead of 'markup'.
'description' => [
'#type' => 'inline_template',
'#template' => '{{ description }}',
'#context' => [
'description' => $handler->options['description'],
],
],
];
$variables['caption_needed'] = TRUE;
}
$variables['responsive'] = FALSE;
// If table has headers, and it should react responsively to columns hidden
// with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM
// and RESPONSIVE_PRIORITY_LOW, add the tableresponsive behaviors.
if (isset($variables['header']) && $responsive) {
$variables['view']->element['#attached']['library'][] = 'core/drupal.tableresponsive';
// Add 'responsive-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
$variables['responsive'] = TRUE;
}
}
/**
* Prepares variables for views table templates.
*
* Default template: views-view-work-time-sheet.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_view_work_time_sheet(array &$variables) {
$view = $variables['view'];
// We need the raw data for this grouping, which is passed in as
// $variables['rows']. However, the template also needs to use for
// the rendered fields. We therefore swap the raw data out to a new variable
// and reset $variables['rows'] so that it can get rebuilt.
// Store rows so that they may be used by further preprocess functions.
$result = $variables['result'] = $variables['rows'];
$variables['header'] = [];
$options = $view->style_plugin->options;
$drupalSettings = $options;
$handler = $view->style_plugin;
$fields = &$view->field;
$columns = array_keys($fields);
$columns = array_combine($columns, $columns);
$active = !empty($handler->active) ? $handler->active : '';
// A boolean variable which stores whether the table has a responsive class.
$responsive = FALSE;
// For the actual site we want to not render full URLs, because this would
// make pagers cacheable per URL, which is problematic in blocks, for example.
// For the actual live preview though the javascript relies on properly
// working URLs.
$drupalSettings['route_name'] = $variables['route_name'] = !empty($view->live_preview) ? '<current>' : \Drupal::routeMatch()->getRouteName();
$type = $view->filter["type"];
$drupalSettings['reference_type'] = $variables['reference_type'] = !empty($type) ? $type->getEntityType() : NULL;
foreach ($columns as $field => $column) {
// Create a second variable, so we can easily find what fields we have and
// what the CSS classes should be.
$variables['fields'][$field] = Html::cleanCssIdentifier($field);
if ($active == $field) {
$variables['fields'][$field] .= ' is-active';
}
// Render the header labels.
if ($field == $column && empty($fields[$field]->options['exclude'])) {
$label = !empty($fields[$field]) ? $fields[$field]->label() : '';
$variables['header'][$field]['content'] = $label;
$variables['footer'][$field]['content'] = $field == array_key_first($columns) ? t('Total') : '';
$variables['header'][$field]['default_classes'] = $fields[$field]->options['element_default_classes'];
// Set up the header label class.
$variables['header'][$field]['attributes'] = [];
$class = $fields[$field]->elementLabelClasses(0);
if ($class) {
$variables['header'][$field]['attributes']['class'][] = $class;
}
// Add a header label wrapper if one was selected.
if (isset($variables['header'][$field]['content'])) {
$element_label_type = $fields[$field]->elementLabelType(TRUE, TRUE);
if ($element_label_type) {
$variables['header'][$field]['wrapper_element'] = $element_label_type;
}
// Improves accessibility of complex tables.
$variables['header'][$field]['attributes']['id'] = Html::getUniqueId('view-' . $field . '-table-column');
}
$variables['header'][$field]['attributes'] = new Attribute($variables['header'][$field]['attributes']);
}
// Render each field into its appropriate column.
foreach ($result as $num => $row) {
// Skip building the attributes and content if the field is to be excluded
// from the display.
if (!empty($fields[$field]->options['exclude'])) {
continue;
}
// Reference to the column in the loop to make the code easier to read.
$column_reference =& $variables['rows'][$num]['columns'][$column];
$column_reference['default_classes'] = $fields[$field]->options['element_default_classes'];
// Set the field key to the column, so it can be used for adding classes
// in a template.
$column_reference['fields'][] = $variables['fields'][$field];
// Add field classes.
if (!isset($column_reference['attributes'])) {
$column_reference['attributes'] = [];
}
if ($classes = $fields[$field]->elementClasses($num)) {
$column_reference['attributes']['class'][] = $classes;
}
$reference_id = $options["reference_id"] ? $row['#row']->_entity->get($options["reference_id"])->value : NULL;
if (!empty($options['price_field']) && $field == $options['price_field']) {
$column_reference['attributes']['class'][] = 'price_field';
$column_reference['attributes']['class'][] = 'price-field-' . $reference_id;
}
// Improves accessibility of complex tables.
if (isset($variables['header'][$field]['attributes']['id'])) {
$column_reference['attributes']['headers'] = [$variables['header'][$field]['attributes']['id']];
}
if (!empty($fields[$field])) {
$field_output = $handler->getField($num, $field);
if (in_array($field, ['title', 'name'])) {
$label = str_replace('"', '', trim(strip_tags($field_output)));
$column_reference['attributes']['class'][] = 'timesheet_label';
$column_reference['attributes']['data-label'] = $label;
$column_reference['attributes']['data-reference'] = $reference_id;
}
$column_reference['wrapper_element'] = $fields[$field]->elementType(TRUE, TRUE);
if (!isset($column_reference['content'])) {
$column_reference['content'] = [];
}
// Only bother with separators and stuff if the field shows up.
// Place the field into the column, along with an optional separator.
if (trim($field_output) != '') {
if (!empty($column_reference['content']) && !empty($options['info'][$column]['separator'])) {
$column_reference['content'][] = [
'separator' => ['#markup' => $options['info'][$column]['separator']],
'field_output' => ['#markup' => $field_output],
];
}
else {
$column_reference['content'][] = [
'field_output' => ['#markup' => $field_output],
];
}
}
}
$column_reference['attributes'] = new Attribute($column_reference['attributes']);
}
}
// Get year month.
$month = date('m');
$year = date('Y');
$yearMonth = \Drupal::request()->get('month');
if (!empty($yearMonth)) {
[$year, $month] = explode('-', $yearMonth);
}
$holidays = work_time_get_holidays($options['holidays'], $year);
$drupalSettings['year'] = $year;
$drupalSettings['month'] = $month;
$variables['month'] = "$year-$month";
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
// Render header day in month.
foreach (range(1, $days_in_month) as $day) {
$day = str_pad($day, 2, '0', STR_PAD_LEFT);
$date = "$year-$month-$day";
$dayOfWeek = date('N', strtotime($date));
$variables['header'][$day] = [
'content' => $day,
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-header-' . $date),
'data-date' => $date,
'class' => [
'day-' . $dayOfWeek,
$dayOfWeek == 7 ? 'bg-danger-subtle' : '',
],
]),
];
$variables['footer'][$day] = [
'content' => '',
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-footer-' . $date),
'data-date' => $date,
'class' => [
'day-' . $dayOfWeek,
'total-col-' . $day,
$dayOfWeek == 7 ? 'bg-danger-subtle' : '',
],
]),
];
}
$variables['header']['total'] = [
'content' => t('Total'),
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-header-total'),
'class' => ['time-total'],
]),
];
$variables['footer']['total'] = [
'content' => '',
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-footer-total'),
'class' => ['total-total-time'],
]),
];
if (!empty($options['price_field'])) {
$variables['header']['price'] = [
'content' => t('Price'),
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-header-price'),
'class' => ['price-total'],
]),
];
$variables['footer']['price'] = [
'content' => '',
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-footer-price'),
'class' => ['total-total-price'],
]),
];
}
$tableWorkTime = [];
foreach ($variables['rows'] as $num => $row) {
$variables['rows'][$num]['attributes'] = [];
$entity = $row['#row']?->_entity;
$reference_id = null;
if($entity && !empty($options["reference_id"])){
$reference_id = $entity->get($options["reference_id"])?->value;
}
foreach (range(1, $days_in_month) as $day) {
$day = str_pad($day, 2, '0', STR_PAD_LEFT);
$date = "$year-$month-$day";
$dayOfWeek = date('N', strtotime($date));
$isHoliday = in_array($date, $holidays) ? t('Holiday') : FALSE;
$variables["rows"][$num]["columns"][$day] = [
'content' => $tableWorkTime[$num][$day] ?? '',
'attributes' => new Attribute([
'id' => 'work-time-' . $reference_id . '-' . $date,
'data-date' => $date,
'data-day' => $day,
'data-id' => $reference_id,
'class' => [
'timekeeper',
'in-week-' . $dayOfWeek,
$isHoliday || $dayOfWeek == 6 ? 'text-bg-light text-danger' : '',
$isHoliday ? 'table-success' : '',
'row-' . $reference_id,
'day-' . $day,
$dayOfWeek == 7 ? 'bg-danger-subtle' : '',
],
]),
];
}
$variables["rows"][$num]["columns"]['total'] = [
'content' => t('Total'),
'attributes' => new Attribute([
'id' => 'total-work-time-' . $reference_id . '-' . $date,
'data-total' => 'time',
'class' => ['time-total', 'total-row-' . $reference_id],
]),
];
if (!empty($options['price_field'])) {
$variables["rows"][$num]["columns"]['price'] = [
'content' => t('Price'),
'attributes' => new Attribute([
'id' => 'price-work-time-' . $reference_id . '-' . $date,
'data-price-id' => 'price',
'class' => ['price-total', 'price-row-' . $reference_id],
]),
];
}
}
if (empty($variables['rows']) && !empty($options['empty_table'])) {
$build = $view->display_handler->renderArea('empty');
$row = &$variables['rows'][0];
$row['columns'][0]['content'][0]['field_output'] = $build;
$row['attributes'] = new Attribute(['class' => ['odd']]);
// Calculate the amounts of rows with output.
$row['columns'][0]['attributes'] = new Attribute([
'colspan' => count($variables['header']),
'class' => ['views-empty'],
]);
}
$variables['sticky'] = FALSE;
if (!empty($options['sticky'])) {
$variables['view']->element['#attached']['library'][] = 'core/drupal.tableheader';
$variables['sticky'] = TRUE;
}
// Add the caption to the list if set.
if (!empty($handler->options['caption'])) {
$variables['caption'] = ['#markup' => $handler->options['caption']];
$variables['caption_needed'] = TRUE;
}
elseif (!empty($variables['title'])) {
$variables['caption'] = ['#markup' => $variables['title']];
$variables['caption_needed'] = TRUE;
}
else {
$variables['caption'] = '';
$variables['caption_needed'] = FALSE;
}
// For backwards compatibility, initialize the 'summary' and 'description'
// variables, although core templates now all use 'summary_element' instead.
$variables['summary'] = $handler->options['summary'] ?? '';
$variables['description'] = $handler->options['description'] ?? '';
if (!empty($handler->options['summary']) || !empty($handler->options['description'])) {
$variables['summary_element'] = [
'#type' => 'details',
'#title' => $handler->options['summary'],
// To ensure that the description is properly escaped during rendering,
// use an 'inline_template' to let Twig do its magic, instead of 'markup'.
'description' => [
'#type' => 'inline_template',
'#template' => '{{ description }}',
'#context' => [
'description' => $handler->options['description'],
],
],
];
$variables['caption_needed'] = TRUE;
}
$variables['responsive'] = FALSE;
// If the table has headers, and it should react responsively to columns
// hidden with the classes represented by constant RESPONSIVE_PRIORITY_MEDIUM
// and RESPONSIVE_PRIORITY_LOW, add the tableresponsive behaviors.
if (isset($variables['header']) && $responsive) {
$variables['view']->element['#attached']['library'][] = 'core/drupal.tableresponsive';
// Add 'responsive-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
$variables['responsive'] = TRUE;
}
$variables['view']->element['#attached']['library'][] = 'work_time/work-time-sheet';
$variables['view']->element['#attached']['drupalSettings']['work_time'] = $drupalSettings;
}
/**
* {@inheritdoc}
*/
function work_time_get_holidays($holidays, $year = '') {
$config = \Drupal::config('work_time.settings');
$generalHoliday = $config->get('holidays');
if (!empty($holidays)) {
$generalHoliday .= ',' . $holidays;
}
$holidays = explode(',', preg_replace('/\s+/', '', $generalHoliday));
if (!empty($year) && !empty($holidays)) {
foreach ($holidays as &$holiday) {
if (strlen($holiday < 6)) {
$holiday = "$holiday-$year";
}
$holiday = date('Y-m-d', strtotime($holiday));
}
}
return $holidays;
}
/**
* Prepares variables for views table templates.
*
* Default template: views-view-work-timekeeper.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_view_work_timekeeper(array &$variables) {
$view = $variables['view'];
// We need the raw data for this grouping, which is passed in
// as $variables['rows'].
// However, the template also needs to use for the rendered fields. We
// therefore swap the raw data out to a new variable and reset $variables['rows']
// so that it can get rebuilt.
// Store rows so that they may be used by further preprocess functions.
$result = $variables['result'] = $variables['rows'];
$variables['rows'] = [];
$variables['header'] = [];
$options = $view->style_plugin->options;
$handler = $view->style_plugin;
$fields = &$view->field;
$columns = array_keys($fields);
$columns = array_combine($columns, $columns);
$active = !empty($handler->active) ? $handler->active : '';
$order = !empty($handler->order) ? $handler->order : 'asc';
// A boolean variable which stores whether the table has a responsive class.
$responsive = FALSE;
// For the actual site we want to not render full URLs, because this would
// make pagers cacheable per URL, which is problematic in blocks, for example.
// For the actual live preview though the javascript relies on properly
// working URLs.
$route_name = !empty($view->live_preview) ? '<current>' : '<none>';
$query = TableSort::getQueryParameters(\Drupal::request());
if (isset($view->exposed_raw_input)) {
$query += $view->exposed_raw_input;
}
// A boolean to store whether the table's header has any labels.
$has_header_labels = FALSE;
foreach ($columns as $field => $column) {
// Create a second variable, so we can easily find what fields we have and
// what the CSS classes should be.
$variables['fields'][$field] = Html::cleanCssIdentifier($field);
if ($active == $field) {
$variables['fields'][$field] .= ' is-active';
}
// Render the header labels.
if ($field == $column && empty($fields[$field]->options['exclude'])) {
$label = !empty($fields[$field]) ? $fields[$field]->label() : '';
if (empty($options['info'][$field]['sortable']) || !$fields[$field]->clickSortable()) {
$variables['header'][$field]['content'] = $label;
}
else {
$initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
if ($active == $field) {
$initial = ($order == 'asc') ? 'desc' : 'asc';
}
$title = t('sort by @s', ['@s' => $label]);
if ($active == $field) {
$variables['header'][$field]['sort_indicator'] = [
'#theme' => 'tablesort_indicator',
'#style' => $initial,
];
}
$query['order'] = $field;
$query['sort'] = $initial;
$link_options = [
'query' => $query,
];
$url = new Url($route_name, [], $link_options);
$variables['header'][$field]['url'] = $url->toString();
$variables['header'][$field]['content'] = $label;
$variables['header'][$field]['title'] = $title;
}
$variables['header'][$field]['default_classes'] = $fields[$field]->options['element_default_classes'];
// Set up the header label class.
$variables['header'][$field]['attributes'] = [];
$class = $fields[$field]->elementLabelClasses(0);
if ($class) {
$variables['header'][$field]['attributes']['class'][] = $class;
}
// Add responsive header classes.
if (!empty($options['info'][$field]['responsive'])) {
$variables['header'][$field]['attributes']['class'][] = $options['info'][$field]['responsive'];
$responsive = TRUE;
}
// Add a CSS align class to each field if one was set.
if (!empty($options['info'][$field]['align'])) {
$variables['header'][$field]['attributes']['class'][] = Html::cleanCssIdentifier($options['info'][$field]['align']);
}
// Add a header label wrapper if one was selected.
if ($variables['header'][$field]['content']) {
$element_label_type = $fields[$field]->elementLabelType(TRUE, TRUE);
if ($element_label_type) {
$variables['header'][$field]['wrapper_element'] = $element_label_type;
}
// Improves accessibility of complex tables.
$variables['header'][$field]['attributes']['id'] = Html::getUniqueId('view-' . $field . '-table-column');
}
$variables['header'][$field]['attributes'] = new Attribute($variables['header'][$field]['attributes']);
}
// Add a CSS align class to each field if one was set.
if (!empty($options['info'][$field]['align'])) {
$variables['fields'][$field] .= ' ' . Html::cleanCssIdentifier($options['info'][$field]['align']);
}
// Render each field into its appropriate column.
foreach ($result as $num => $row) {
// Skip building the attributes and content if the field is to be excluded
// from the display.
if (!empty($fields[$field]->options['exclude'])) {
continue;
}
// Reference to the column in the loop to make the code easier to read.
$column_reference =& $variables['rows'][$num]['columns'][$column];
$column_reference['default_classes'] = $fields[$field]->options['element_default_classes'];
// Set the field key to the column so it can be used for adding classes
// in a template.
$column_reference['fields'][] = $variables['fields'][$field];
// Add field classes.
if (!isset($column_reference['attributes'])) {
$column_reference['attributes'] = [];
}
if ($classes = $fields[$field]->elementClasses($num)) {
$column_reference['attributes']['class'][] = $classes;
}
// Add responsive header classes.
if (!empty($options['info'][$field]['responsive'])) {
$column_reference['attributes']['class'][] = $options['info'][$field]['responsive'];
}
// Improves accessibility of complex tables.
if (isset($variables['header'][$field]['attributes']['id'])) {
$column_reference['attributes']['headers'] = [$variables['header'][$field]['attributes']['id']];
}
if (!empty($fields[$field])) {
$field_output = $handler->getField($num, $field);
$column_reference['wrapper_element'] = $fields[$field]->elementType(TRUE, TRUE);
if (!isset($column_reference['content'])) {
$column_reference['content'] = [];
}
// Only bother with separators and stuff if the field shows up.
// Place the field into the column, along with an optional separator.
if (trim($field_output) != '') {
if (!empty($column_reference['content']) && !empty($options['info'][$column]['separator'])) {
$column_reference['content'][] = [
'separator' => ['#markup' => $options['info'][$column]['separator']],
'field_output' => ['#markup' => $field_output],
];
}
else {
$column_reference['content'][] = [
'field_output' => ['#markup' => $field_output],
];
}
}
}
$column_reference['attributes'] = new Attribute($column_reference['attributes']);
}
}
// Get year month.
$month = date('m');
$year = date('Y');
$yearMonth = \Drupal::request()->get('month');
if (!empty($yearMonth)) {
[$year, $month] = explode('-', $yearMonth);
}
$holidays = work_time_get_holidays($options['holidays'], $year);
$drupalSettings['year'] = $year;
$drupalSettings['month'] = $month;
$variables['month'] = "$year-$month";
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
// Render header day in month.
foreach (range(1, $days_in_month) as $day) {
$day = str_pad($day, 2, '0', STR_PAD_LEFT);
$date = "$year-$month-$day";
$dayOfWeek = date('N', strtotime($date));
$variables['header'][$day] = [
'content' => $day,
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-header-' . $date),
'data-date' => $date,
'class' => [
'day-' . $dayOfWeek,
$dayOfWeek == 7 ? 'bg-danger-subtle' : '',
],
]),
];
$variables['footer'][$day] = [
'content' => '',
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-footer-' . $date),
'data-date' => $date,
'class' => [
'day-' . $dayOfWeek,
'total-col-' . $day,
$dayOfWeek == 7 ? 'bg-danger-subtle' : '',
],
]),
];
}
$variables['header']['overtime'] = [
'content' => t('Overtime'),
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-header-overtime'),
'class' => ['time-overtime'],
]),
];
$variables['header']['total'] = [
'content' => t('Total'),
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-header-total'),
'class' => ['time-total'],
]),
];
$variables['footer']['overtime'] = [
'content' => '',
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-footer-overtime'),
'class' => ['time-total-overtime'],
]),
];
$variables['footer']['total'] = [
'content' => '',
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-footer-total'),
'class' => ['total-total-time'],
]),
];
if (!empty($options['price_field'])) {
$variables['header']['price'] = [
'content' => t('Price'),
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-header-price'),
'class' => ['price-total'],
]),
];
$variables['footer']['price'] = [
'content' => '',
'attributes' => new Attribute([
'id' => Html::getUniqueId('work-time-footer-price'),
'class' => ['total-total-price'],
]),
];
}
$tableWorkTime = [];
foreach ($variables['rows'] as $num => $row) {
$variables['rows'][$num]['attributes'] = [];
$entity = $row['#row']?->_entity;
$reference_id = null;
if($entity && !empty($options["reference_id"])){
$reference_id = $entity->get($options["reference_id"])?->value;
}
foreach (range(1, $days_in_month) as $day) {
$day = str_pad($day, 2, '0', STR_PAD_LEFT);
$date = "$year-$month-$day";
$dayOfWeek = date('N', strtotime($date));
$isHoliday = in_array($date, $holidays) ? t('Holiday') : FALSE;
$variables["rows"][$num]["columns"][$day] = [
'content' => $tableWorkTime[$num][$day] ?? '',
'attributes' => new Attribute([
'id' => 'work-time-' . $reference_id . '-' . $date,
'data-date' => $date,
'data-day' => $day,
'data-id' => $reference_id,
'class' => [
'timekeeper',
'in-week-' . $dayOfWeek,
$isHoliday || $dayOfWeek == 6 ? 'text-bg-light text-danger' : '',
$isHoliday ? 'table-success' : '',
'row-' . $reference_id,
'day-' . $day,
$dayOfWeek == 7 ? 'bg-danger-subtle' : '',
],
]),
];
}
$variables["rows"][$num]["columns"]['overtime'] = [
'content' => t('Overtime'),
'attributes' => new Attribute([
'id' => 'overtime-work-time-' . $reference_id . '-' . $date,
'data-overtime' => 'time',
'class' => ['time-overtime', 'overtime-row-' . $reference_id],
]),
];
$variables["rows"][$num]["columns"]['total'] = [
'content' => t('Total'),
'attributes' => new Attribute([
'id' => 'total-work-time-' . $reference_id . '-' . $date,
'data-total' => 'time',
'class' => ['time-total', 'total-row-' . $reference_id],
]),
];
if (!empty($options['price_field'])) {
$variables["rows"][$num]["columns"]['price'] = [
'content' => t('Price'),
'attributes' => new Attribute([
'id' => 'price-work-time-' . $reference_id . '-' . $date,
'data-price-id' => 'price',
'class' => ['price-total', 'price-row-' . $reference_id],
]),
];
}
}
if (empty($variables['rows']) && !empty($options['empty_table'])) {
$build = $view->display_handler->renderArea('empty');
$variables['rows'][0]['columns'][0]['content'][0]['field_output'] = $build;
$variables['rows'][0]['attributes'] = new Attribute(['class' => ['odd']]);
// Calculate the amounts of rows with output.
$variables['rows'][0]['columns'][0]['attributes'] = new Attribute([
'colspan' => count($variables['header']),
'class' => ['views-empty'],
]);
}
$variables['sticky'] = FALSE;
if (!empty($options['sticky'])) {
$variables['view']->element['#attached']['library'][] = 'core/drupal.tableheader';
$variables['sticky'] = TRUE;
}
// Add the caption to the list if set.
if (!empty($handler->options['caption'])) {
$variables['caption'] = ['#markup' => $handler->options['caption']];
$variables['caption_needed'] = TRUE;
}
elseif (!empty($variables['title'])) {
$variables['caption'] = ['#markup' => $variables['title']];
$variables['caption_needed'] = TRUE;
}
else {
$variables['caption'] = '';
$variables['caption_needed'] = FALSE;
}
// For backwards compatibility, initialize the 'summary' and 'description'
// variables, although core templates now all use 'summary_element' instead.
$variables['summary'] = $handler->options['summary'];
$variables['description'] = $handler->options['description'];
if (!empty($handler->options['summary']) || !empty($handler->options['description'])) {
$variables['summary_element'] = [
'#type' => 'details',
'#title' => $handler->options['summary'],
// To ensure that the description is properly escaped during rendering,
// use an 'inline_template' to let Twig do its magic, instead of 'markup'.
'description' => [
'#type' => 'inline_template',
'#template' => '{{ description }}',
'#context' => [
'description' => $handler->options['description'],
],
],
];
$variables['caption_needed'] = TRUE;
}
$variables['responsive'] = FALSE;
// If the table has headers, & it should react responsively to columns hidden
// with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM
// and RESPONSIVE_PRIORITY_LOW, add the tableresponsive behaviors.
if (isset($variables['header']) && $responsive) {
$variables['view']->element['#attached']['library'][] = 'core/drupal.tableresponsive';
// Add 'responsive-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
$variables['responsive'] = TRUE;
}
$variables["attributes"]['class'][] = 'work-timekeeper';
$variables['view']->element['#attached']['library'][] = 'work_time/work-time-keeper';
}
