Skip to content

Commit

Permalink
Allow numbers in custom objects system names
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Jan 23, 2025
1 parent be2b2f5 commit ea302ac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/Glpi/CustomObject/AbstractDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,11 @@ protected function prepareInput(array $input): array|bool
$has_errors = false;

if (array_key_exists('system_name', $input)) {
if (!is_string($input['system_name']) || preg_match('/^[a-z]+$/i', $input['system_name']) !== 1) {
// 1. Must start with a letter.
// 2. Must contain only letters or numbers.
$system_name_pattern = '/^[a-z][a-z0-9]*$/i';

if (!is_string($input['system_name']) || preg_match($system_name_pattern, $input['system_name']) !== 1) {
Session::addMessageAfterRedirect(
htmlescape(sprintf(
__('The following field has an incorrect value: "%s".'),
Expand Down
13 changes: 4 additions & 9 deletions src/Glpi/Search/Provider/SQLProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4789,15 +4789,10 @@ public static function constructData(array &$data, $onlycount = false)

// Parse data
foreach ($newrow['raw'] as $key => $val) {
if (preg_match('/ITEM(_(\w[^\d]+))?_(\d+)(_(.+))?/', $key, $matches)) {
$j = $matches[3];
if (isset($matches[2]) && !empty($matches[2])) {
$j = $matches[2] . '_' . $matches[3];
}
$fieldname = 'name';
if (isset($matches[5])) {
$fieldname = $matches[5];
}
$matches = [];
if (preg_match('/^ITEM(_(?<itemtype>[a-z]\w*))?_(?<num>\d+)(_(?<fieldname>.+))?$/i', $key, $matches)) {
$j = (isset($matches['itemtype']) ? $matches['itemtype'] . '_' : '') . $matches['num'];
$fieldname = $matches['fieldname'] ?? 'name';

// No Group_concat case
if ($fieldname == 'content' || !is_string($val) || strpos($val, \Search::LONGSEP) === false) {
Expand Down
24 changes: 16 additions & 8 deletions templates/pages/admin/customobjects/main.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,38 @@
</script>
{% endif %}
<script type="module">
$('#mainformtable input[name="system_name"]').on('input', () => {
$('#mainformtable input[name="system_name"]').on('change', () => {
if ($('input[name="system_name"]').val() === '') {
$('input[name="system_name"]').data('manually_changed', false);
} else {
$('input[name="system_name"]').data('manually_changed', true);
}
});
function autoUpdateNameField() {
if ({{ not item.isNewItem() ? 'true' : 'false' }} || $('#mainformtable input[name="system_name"]').data('manually_changed')) {
return;
}
$('#mainformtable input[name="system_name"]').on('input', () => {
const reserved_names = {{ reserved_system_names|json_encode()|raw }}.map((n) => n.toLowerCase());
const existing_names = {{ existing_system_names|json_encode()|raw }}.map((n) => n.toLowerCase());
$('#mainformtable input[name="system_name"]').val(
$('#mainformtable input[name="label"]').val().normalize('NFD').replace(/[^a-z]/gi, '')
);
const system_name = $('#mainformtable input[name="system_name"]').val().toLowerCase();
if (reserved_names.includes(system_name) || system_name.endsWith('type') || system_name.endsWith('model')) {
$('#mainformtable input[name="system_name"]').get(0).setCustomValidity(__('The system name is a reserved name. Please enter a different label or manually change the system name.'));
} else if (existing_names.includes(system_name)) {
$('#mainformtable input[name="system_name"]').get(0).setCustomValidity(__('The system name is already in use. Please enter a different label or manually change the system name.'));
} else if (/^[a-z][a-z0-9]*$/i.test(system_name) === false) {
{# See pattern used in \Glpi\CustomObject\AbstractDefinition::prepareInput() #}
$('#mainformtable input[name="system_name"]').get(0).setCustomValidity(__('The system name is invalid. It must start with a letter and contain only alphanumeric chars.'));
} else {
$('#mainformtable input[name="system_name"]').get(0).setCustomValidity('');
}
});
function autoUpdateNameField() {
if ({{ not item.isNewItem() ? 'true' : 'false' }} || $('#mainformtable input[name="system_name"]').data('manually_changed')) {
return;
}
$('#mainformtable input[name="system_name"]').val(
$('#mainformtable input[name="label"]').val().normalize('NFD').replace(/[^a-z0-9]/gi, '')
);
$('#mainformtable input[name="system_name"]').trigger('input');
};
$('#mainformtable input[name="label"]').on('input', () => {
autoUpdateNameField();
Expand Down

0 comments on commit ea302ac

Please sign in to comment.