Skip to content

Commit

Permalink
Generate itemtypes without numbers; fixes pluginsGLPI#305
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Mar 7, 2019
1 parent 9db449e commit f0461af
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,22 @@ function prepareInputForAdd($input) {
}
}

// construct field name by processing label
// (remove non alphanumeric char and any trailing spaces)
$input['name'] = strtolower(preg_replace("/[^\da-z]/i", "", preg_replace('/s*$/', '', $input['label'])));
// if empty, uses a random number
// Construct field name by processing label
$name = strtolower($input['label']);
// 1. remove trailing "s" (plural forms)
$name = preg_replace('/s*$/', '', $name);
// 2. keep only alphanum
$input['name'] = preg_replace('/[^\da-z]/i', '', $name);
// 3. if empty, uses a random number
if (strlen($input['name']) == 0) {
$input['name'] = rand();
}
// 4. replace numbers by letters
$name = str_replace(
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'],
$name
);

//check for already existing container with same name
$found = $this->find(['name' => $input['name']]);
Expand Down

0 comments on commit f0461af

Please sign in to comment.