Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Credit vouchers can be restricted to incidents and/or requests (both enabled by default)

### Changed

- Make the plugin compatible with the Content Security Policy

## [1.16.1] - 2026-10-27

### Fixed
Expand Down
141 changes: 141 additions & 0 deletions public/js/ticket_credit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* -------------------------------------------------------------------------
* Credit plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Credit.
*
* Credit is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Credit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Credit. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @author François Legastelois
* @copyright Copyright (C) 2017-2023 by Credit plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/credit
* -------------------------------------------------------------------------
*/

function creditInitConsume(root) {
const rand = root.dataset.creditConsumeRand;
const quantityUrl = root.dataset.creditQuantityUrl;

function showCreditDiv() {
const creditVoucherBlock = document.getElementById('creditvoucherblock' + rand);
if (creditVoucherBlock) {
const isHidden = creditVoucherBlock.style.display !== 'none';
creditVoucherBlock.style.display = isHidden ? 'none' : 'block';
var consumeblock = document.querySelector('[name="credit_consume_' + rand + '"]');
var quantity = consumeblock.querySelector('[id*="plugin_credit_quantity_' + rand + '"]');
if (quantity) {
quantity.disabled = isHidden;
}
}
}

function getQuantityRemaining(val) {
var consumeblock = document.querySelector('[name="credit_consume_' + rand + '"]');
var quantity = consumeblock.querySelector('[id*="plugin_credit_quantity_' + rand + '"]');
var quantity_field = quantity.closest('.col-6');
var quantity_label = consumeblock.querySelector('[for*="plugin_credit_quantity_' + rand + '"]');
if (val == 0) {
quantity_field.classList.add('d-none');
quantity.disabled = true;
quantity.max = 0;
quantity.value = 0;
quantity_label.querySelector('span.form-help').setAttribute('data-bs-title', __('Maximum consumable quantity', 'credit') + ' : 0 ');
quantity_label.querySelector('span.form-help').setAttribute('data-bs-content', __('Maximum consumable quantity', 'credit') + ' : 0 ');
} else {
quantity_field.classList.remove('d-none');
quantity.disabled = false;

$.ajax({
type: 'POST',
url: quantityUrl,
data: {
entity: val,
}
}).done(function (data) {
quantity.max = data;
quantity.value = 1;
quantity_label.querySelector('span.form-help').setAttribute('data-bs-title', __('Maximum consumable quantity', 'credit') + ' : <b>' + data);
quantity_label.querySelector('span.form-help').setAttribute('data-bs-content', __('Maximum consumable quantity', 'credit') + ' : <b>' + data);
});
}
}

document.getElementById('plugin_credit_consumed_voucher_' + rand).addEventListener('change', function () {
showCreditDiv(this.value);
});
$('#dropdown_plugin_credit_entities_id' + rand).on('change', function () {
getQuantityRemaining(this.value);
});
}

function creditInitForm(root) {
const rand = root.dataset.creditFormRand;
const quantityUrl = root.dataset.creditQuantityUrl;

function getQuantityRemainingForm(val, source) {
var form = source ? source.closest('form') : document.getElementById('creditentity_form' + rand);
if (!form) {
return;
}

var quantity = form.querySelector('[id*="plugin_credit_quantity"]');
var quantity_label = form.querySelector('[for*="plugin_credit_quantity"]');
if (!quantity || !quantity_label) {
return;
}

var form_help = quantity_label.querySelector('span.form-help');
if (!form_help) {
return;
}

if (val == 0) {
quantity.value = 0;
form_help.setAttribute('data-bs-title', __('Maximum consumable quantity', 'credit') + ' : 0 ');
form_help.setAttribute('data-bs-content', __('Maximum consumable quantity', 'credit') + ' : 0 ');
} else {
quantity.closest('.form-field').classList.remove('d-none');
$.ajax({
type: 'POST',
url: quantityUrl,
data: {
entity: val,
}
}).done(function (data) {
quantity.max = data;
quantity.value = 1;
form_help.setAttribute('data-bs-title', __('Maximum consumable quantity', 'credit') + ' : <b>' + data);
form_help.setAttribute('data-bs-content', __('Maximum consumable quantity', 'credit') + ' : <b>' + data);
});
}
}

$('#dropdown_plugin_credit_entities_id' + rand).on('change', function () {
getQuantityRemainingForm(this.value, this);
});
}

document.querySelectorAll('[data-credit-consume-rand]:not([data-credit-consume-ready])').forEach(function (root) {
root.dataset.creditConsumeReady = '';
creditInitConsume(root);
});

document.querySelectorAll('[data-credit-form-rand]:not([data-credit-form-ready])').forEach(function (root) {
root.dataset.creditFormReady = '';
creditInitForm(root);
});
1 change: 1 addition & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function plugin_version_credit()
'author' => '<a href="http://www.teclib.com">Teclib\'</a>',
'license' => 'GPLv3',
'homepage' => 'https://github.com/pluginsGLPI/credit',
'csp_compliant' => true,
'requirements' => [
'glpi' => [
'min' => PLUGIN_CREDIT_MIN_GLPI,
Expand Down
51 changes: 2 additions & 49 deletions templates/tickets/consume.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
consume,
__('Consume a voucher ?', 'credit'),
{
'additional_attributes': {
'onchange': 'showCreditDiv' ~ rand ~ '(this.value)',
},
'rand': rand,
}
) }}
Expand All @@ -59,7 +56,6 @@
{
'entity': entity_id,
'condition': condition,
'on_change': 'getQuantityRemaining' ~ rand ~ '(this.value);',
'field_class': 'col-6 col-sm-6',
'rand': rand,
}
Expand All @@ -85,49 +81,6 @@
</div>
</div>

<script>
function showCreditDiv{{ rand }}() {
const creditVoucherBlock = document.getElementById('creditvoucherblock{{ rand }}');
if (creditVoucherBlock) {
const isHidden = creditVoucherBlock.style.display !== 'none';
creditVoucherBlock.style.display = isHidden ? 'none' : 'block';
var consumeblock = document.querySelector('[name="credit_consume_{{ rand }}"]');
var quantity = consumeblock.querySelector('[id*="plugin_credit_quantity_{{ rand }}"]');
if (quantity) {
quantity.disabled = isHidden;
}
}
}

function getQuantityRemaining{{ rand }}(val) {
var consumeblock = document.querySelector('[name="credit_consume_{{ rand }}"]');
var quantity = consumeblock.querySelector('[id*="plugin_credit_quantity_{{ rand }}"]');
var quantity_field = quantity.closest('.col-6');
var quantity_label = consumeblock.querySelector('[for*="plugin_credit_quantity_{{ rand }}"]');
if (val == 0) {
quantity_field.classList.add('d-none');
quantity.disabled = true;
quantity.max = 0;
quantity.value = 0;
quantity_label.querySelector('span.form-help').setAttribute('data-bs-title', __('Maximum consumable quantity', 'credit') + ' : 0 ');
quantity_label.querySelector('span.form-help').setAttribute('data-bs-content', __('Maximum consumable quantity', 'credit') + ' : 0 ');
} else {
quantity_field.classList.remove('d-none');
quantity.disabled = false;

$.ajax({
type: 'POST',
url: '{{ path('plugins/credit/ajax/dropdownQuantity.php') }}',
data: {
entity: val,
}
}).done(function (data) {
quantity.max = data;
quantity.value = 1;
quantity_label.querySelector('span.form-help').setAttribute('data-bs-title', __('Maximum consumable quantity', 'credit') + ' : <b>' + data);
quantity_label.querySelector('span.form-help').setAttribute('data-bs-content', __('Maximum consumable quantity', 'credit') + ' : <b>' + data);
});
}
}
</script>
<span hidden data-credit-consume-rand="{{ rand }}" data-credit-quantity-url="{{ path('plugins/credit/ajax/dropdownQuantity.php') }}"></span>
{{ call('Html::script', ['/plugins/credit/js/ticket_credit.js', {'version': constant('PLUGIN_CREDIT_VERSION')}, false])|raw }}
</div>
43 changes: 3 additions & 40 deletions templates/tickets/form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
{
'entity': entity_id,
'condition': conditions,
'on_change': 'getQuantityRemainingForm' ~ rand ~ '(this.value, this);',
'field_class': 'col-4 col-sm-4',
'rand': rand,
}
) }}

Expand All @@ -65,45 +65,8 @@
}
) }}

<script>
function getQuantityRemainingForm{{ rand }}(val, source) {
var form = source ? source.closest('form') : document.getElementById('creditentity_form{{ rand }}');
if (!form) {
return;
}

var quantity = form.querySelector('[id*="plugin_credit_quantity"]');
var quantity_label = form.querySelector('[for*="plugin_credit_quantity"]');
if (!quantity || !quantity_label) {
return;
}

var form_help = quantity_label.querySelector('span.form-help');
if (!form_help) {
return;
}

if (val == 0) {
quantity.value = 0;
form_help.setAttribute('data-bs-title', __('Maximum consumable quantity', 'credit') + ' : 0 ');
form_help.setAttribute('data-bs-content', __('Maximum consumable quantity', 'credit') + ' : 0 ');
} else {
quantity.closest('.form-field').classList.remove('d-none');
$.ajax({
type: 'POST',
url: '{{ path('plugins/credit/ajax/dropdownQuantity.php') }}',
data: {
entity: val,
}
}).done(function (data) {
quantity.max = data;
quantity.value = 1;
form_help.setAttribute('data-bs-title', __('Maximum consumable quantity', 'credit') + ' : <b>' + data);
form_help.setAttribute('data-bs-content', __('Maximum consumable quantity', 'credit') + ' : <b>' + data);
});
}
}
</script>
<span hidden data-credit-form-rand="{{ rand }}" data-credit-quantity-url="{{ path('plugins/credit/ajax/dropdownQuantity.php') }}"></span>
{{ call('Html::script', ['/plugins/credit/js/ticket_credit.js', {'version': constant('PLUGIN_CREDIT_VERSION')}, false])|raw }}
<div class="hstack gap-1">
<div class="pe-5 ms-auto mb-2">
<button class="btn btn-primary" type="submit" name="add" value="1">
Expand Down