2021-03-10 09:43:36 +01:00
|
|
|
<?php
|
|
|
|
use Cake\Utility\Inflector;
|
|
|
|
|
|
|
|
$filteringForm = $this->Bootstrap->table(
|
|
|
|
[
|
|
|
|
'small' => true,
|
|
|
|
'striped' => false,
|
|
|
|
'hover' => false,
|
|
|
|
'tableClass' => ['indexFilteringTable'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'fields' => [
|
|
|
|
__('Field'),
|
|
|
|
__('Operator'),
|
|
|
|
[
|
|
|
|
'labelHtml' => sprintf('%s %s',
|
|
|
|
__('Value'),
|
2021-08-30 15:11:21 +02:00
|
|
|
sprintf('<sup class="fa fa-info" title="%s"><sup>', __('Supports strict matches and LIKE matches with the `%` character. Example: `%.com`'))
|
2021-03-10 09:43:36 +01:00
|
|
|
)
|
|
|
|
],
|
|
|
|
__('Action')
|
|
|
|
],
|
|
|
|
'items' => []
|
|
|
|
]);
|
|
|
|
|
2021-08-30 15:11:21 +02:00
|
|
|
if ($taggingEnabled) {
|
|
|
|
$helpText = $this->Bootstrap->genNode('sup', [
|
2021-09-17 17:51:45 +02:00
|
|
|
'class' => ['ms-1 fa fa-info'],
|
2021-08-30 15:11:21 +02:00
|
|
|
'title' => __('Supports negation matches (with the `!` character) and LIKE matches (with the `%` character). Example: `!exportable`, `%able`'),
|
2021-09-17 18:12:24 +02:00
|
|
|
'data-bs-toggle' => 'tooltip',
|
2021-08-30 15:11:21 +02:00
|
|
|
]);
|
|
|
|
$filteringTags = $this->Bootstrap->genNode('h5', [], __('Tags') . $helpText);
|
|
|
|
$filteringTags .= $this->Tag->tags([], [
|
|
|
|
'allTags' => $allTags,
|
|
|
|
'picker' => true,
|
|
|
|
'editable' => false,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$filteringTags = '';
|
|
|
|
}
|
|
|
|
$modalBody = sprintf('%s%s', $filteringForm, $filteringTags);
|
|
|
|
|
2021-03-10 09:43:36 +01:00
|
|
|
|
|
|
|
echo $this->Bootstrap->modal([
|
|
|
|
'title' => __('Filtering options for {0}', Inflector::singularize($this->request->getParam('controller'))),
|
|
|
|
'size' => 'lg',
|
|
|
|
'type' => 'confirm',
|
2021-08-30 15:11:21 +02:00
|
|
|
'bodyHtml' => $modalBody,
|
2021-03-10 09:43:36 +01:00
|
|
|
'confirmText' => __('Filter'),
|
2021-03-23 10:04:06 +01:00
|
|
|
'confirmFunction' => 'filterIndex'
|
2021-03-10 09:43:36 +01:00
|
|
|
]);
|
|
|
|
?>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
$(document).ready(() => {
|
|
|
|
const $filteringTable = $('table.indexFilteringTable')
|
|
|
|
initFilteringTable($filteringTable)
|
|
|
|
})
|
|
|
|
|
2021-03-23 10:04:06 +01:00
|
|
|
function filterIndex(modalObject, tmpApi) {
|
2021-03-10 09:43:36 +01:00
|
|
|
const controller = '<?= $this->request->getParam('controller') ?>';
|
|
|
|
const action = 'index';
|
2021-03-23 10:04:06 +01:00
|
|
|
const $tbody = modalObject.$modal.find('table.indexFilteringTable tbody')
|
2021-03-10 09:43:36 +01:00
|
|
|
const $rows = $tbody.find('tr:not(#controlRow)')
|
|
|
|
const activeFilters = {}
|
|
|
|
$rows.each(function() {
|
|
|
|
const rowData = getDataFromRow($(this))
|
|
|
|
let fullFilter = rowData['name']
|
|
|
|
if (rowData['operator'] == '!=') {
|
|
|
|
fullFilter += ' !='
|
|
|
|
}
|
|
|
|
activeFilters[fullFilter] = rowData['value']
|
|
|
|
})
|
2021-08-30 15:11:21 +02:00
|
|
|
$select = modalObject.$modal.find('select.tag-input')
|
|
|
|
activeFilters['filteringTags'] = $select.select2('data').map(tag => tag.text)
|
|
|
|
const searchParam = jQuery.param(activeFilters);
|
2021-03-10 09:43:36 +01:00
|
|
|
const url = `/${controller}/${action}?${searchParam}`
|
|
|
|
|
|
|
|
const randomValue = getRandomValue()
|
|
|
|
UI.reload(url, $(`#table-container-${randomValue}`), $(`#table-container-${randomValue} table.table`), [{
|
|
|
|
node: $(`#toggleFilterButton-${randomValue}`),
|
|
|
|
config: {}
|
|
|
|
}])
|
|
|
|
}
|
|
|
|
|
|
|
|
function initFilteringTable($filteringTable) {
|
|
|
|
const $controlRow = $filteringTable.find('#controlRow')
|
|
|
|
$filteringTable.find('tbody').empty()
|
|
|
|
addControlRow($filteringTable)
|
|
|
|
const randomValue = getRandomValue()
|
2021-09-01 16:12:56 +02:00
|
|
|
const activeFilters = Object.assign({}, $(`#toggleFilterButton-${randomValue}`).data('activeFilters'))
|
2021-08-30 15:11:21 +02:00
|
|
|
const tags = activeFilters['filteringTags'] !== undefined ? Object.assign({}, activeFilters)['filteringTags'] : []
|
|
|
|
delete activeFilters['filteringTags']
|
2021-03-10 09:43:36 +01:00
|
|
|
for (let [field, value] of Object.entries(activeFilters)) {
|
|
|
|
const fieldParts = field.split(' ')
|
|
|
|
let operator = '='
|
|
|
|
if (fieldParts.length == 2 && fieldParts[1] == '!=') {
|
|
|
|
operator = '!='
|
|
|
|
field = fieldParts[0]
|
|
|
|
} else if (fieldParts.length > 2) {
|
|
|
|
console.error('Field contains multiple spaces. ' + field)
|
|
|
|
}
|
|
|
|
addFilteringRow($filteringTable, field, value, operator)
|
|
|
|
}
|
2021-08-30 15:11:21 +02:00
|
|
|
$select = $filteringTable.closest('.modal-body').find('select.tag-input')
|
|
|
|
let passedTags = []
|
|
|
|
tags.forEach(tagname => {
|
2021-08-31 11:19:15 +02:00
|
|
|
const existingOption = $select.find('option').filter(function() {
|
|
|
|
return $(this).val() === tagname
|
|
|
|
})
|
|
|
|
if (existingOption.length == 0) {
|
2021-08-30 15:11:21 +02:00
|
|
|
passedTags.push(new Option(tagname, tagname, true, true))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
$select
|
|
|
|
.append(passedTags)
|
|
|
|
.val(tags)
|
|
|
|
.trigger('change')
|
2021-03-10 09:43:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function addControlRow($filteringTable) {
|
|
|
|
const availableFilters = <?= json_encode($filters) ?>;
|
2021-09-17 13:04:37 +02:00
|
|
|
const $selectField = $('<select/>').addClass('fieldSelect form-select form-select-sm')
|
2021-03-10 09:43:36 +01:00
|
|
|
availableFilters.forEach(filter => {
|
|
|
|
$selectField.append($('<option/>').text(filter))
|
|
|
|
});
|
2021-09-17 13:04:37 +02:00
|
|
|
const $selectOperator = $('<select/>').addClass('fieldOperator form-select form-select-sm')
|
2021-03-10 09:43:36 +01:00
|
|
|
.append([
|
|
|
|
$('<option/>').text('=').val('='),
|
|
|
|
$('<option/>').text('!=').val('!='),
|
|
|
|
])
|
|
|
|
const $row = $('<tr/>').attr('id', 'controlRow')
|
|
|
|
.append(
|
|
|
|
$('<td/>').append($selectField),
|
|
|
|
$('<td/>').append($selectOperator),
|
|
|
|
$('<td/>').append(
|
|
|
|
$('<input>').attr('type', 'text').addClass('fieldValue form-control form-control-sm')
|
|
|
|
),
|
|
|
|
$('<td/>').append(
|
|
|
|
$('<button/>').attr('type', 'button').addClass('btn btn-sm btn-primary')
|
|
|
|
.append($('<span/>').addClass('fa fa-plus'))
|
|
|
|
.click(addFiltering)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
$filteringTable.append($row)
|
|
|
|
}
|
|
|
|
|
|
|
|
function addFilteringRow($filteringTable, field, value, operator) {
|
2021-09-17 13:04:37 +02:00
|
|
|
const $selectOperator = $('<select/>').addClass('fieldOperator form-select form-select-sm')
|
2021-03-10 09:43:36 +01:00
|
|
|
.append([
|
|
|
|
$('<option/>').text('=').val('='),
|
|
|
|
$('<option/>').text('!=').val('!='),
|
|
|
|
]).val(operator)
|
|
|
|
const $row = $('<tr/>')
|
|
|
|
.append(
|
|
|
|
$('<td/>').text(field).addClass('fieldName').data('fieldName', field),
|
|
|
|
$('<td/>').append($selectOperator),
|
|
|
|
$('<td/>').append(
|
|
|
|
$('<input>').attr('type', 'text').addClass('fieldValue form-control form-control-sm').val(value)
|
|
|
|
),
|
|
|
|
$('<td/>').append(
|
|
|
|
$('<button/>').attr('type', 'button').addClass('btn btn-sm btn-danger')
|
|
|
|
.append($('<span/>').addClass('fa fa-trash'))
|
|
|
|
.click(removeSelf)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
$filteringTable.append($row)
|
|
|
|
const $controlRow = $filteringTable.find('#controlRow')
|
|
|
|
disableOptionFromSelect($controlRow, field)
|
|
|
|
}
|
|
|
|
|
|
|
|
function addFiltering() {
|
|
|
|
const $table = $(this).closest('table.indexFilteringTable')
|
|
|
|
const $controlRow = $table.find('#controlRow')
|
|
|
|
const field = $controlRow.find('select.fieldSelect').val()
|
|
|
|
const value = $controlRow.find('input.fieldValue').val()
|
|
|
|
const operator = $controlRow.find('input.fieldOperator').val()
|
|
|
|
addFilteringRow($table, field, value, operator)
|
|
|
|
$controlRow.find('input.fieldValue').val('')
|
|
|
|
$controlRow.find('select.fieldSelect').val('')
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeSelf() {
|
|
|
|
const $row = $(this).closest('tr')
|
|
|
|
const $controlRow = $row.closest('table.indexFilteringTable').find('#controlRow')
|
|
|
|
const field = $row.data('fieldName')
|
|
|
|
$row.remove()
|
|
|
|
enableOptionFromSelect($controlRow, field)
|
|
|
|
}
|
|
|
|
|
|
|
|
function disableOptionFromSelect($controlRow, optionName) {
|
|
|
|
$controlRow.find('select.fieldSelect option').each(function() {
|
|
|
|
const $option = $(this)
|
|
|
|
if ($option.text() == optionName) {
|
|
|
|
$option.prop('disabled', true)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function enableOptionFromSelect($controlRow, optionName) {
|
|
|
|
$controlRow.find('select.fieldSelect option').each(function() {
|
|
|
|
const $option = $(this)
|
|
|
|
if ($option.text() == optionName) {
|
|
|
|
$option.prop('disabled', false)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDataFromRow($row) {
|
|
|
|
const rowData = {};
|
|
|
|
rowData['name'] = $row.find('td.fieldName').data('fieldName')
|
|
|
|
rowData['operator'] = $row.find('select.fieldOperator').val()
|
|
|
|
rowData['value'] = $row.find('input.fieldValue').val()
|
|
|
|
return rowData
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomValue() {
|
|
|
|
const $container = $('div[id^="table-container-"]')
|
|
|
|
const randomValue = $container.attr('id').split('-')[2]
|
|
|
|
return randomValue
|
|
|
|
}
|
|
|
|
</script>
|