Commit 59936d5f authored by Dmitrii Metelkin's avatar Dmitrii Metelkin Committed by Robert Lyon
Browse files

Bug #826649: move generation of institution name to lib/institution.php



behatnotneeded - backend change so existing tests should be sufficient

Change-Id: I3013ce5d19e043134df327608e13b4b4fa205300
Signed-off-by: default avatarDmitrii Metelkin <dmitriim@catalyst-au.net>
parent db0e9b48
......@@ -707,7 +707,7 @@ function institution_submit(Pieform $form, $values) {
db_begin();
// Update the basic institution record...
if ($add) {
$institution = generate_institution_name($values['displayname']);
$institution = institution_generate_name($values['displayname']);
$newinstitution = new Institution();
$newinstitution->initialise($institution, $values['displayname']);
$institution = $newinstitution->name;
......
......@@ -991,6 +991,41 @@ function institution_display_name($name) {
return hsc(get_field('institution', 'displayname', 'name', $name));
}
/**
* Generate a valid name for the institution.name column, based on the specified display name
*
* @param string $displayname
* @return string
*/
function institution_generate_name($displayname) {
// iconv can crash on strings that are too long, so truncate before converting
$basename = mb_substr($displayname, 0, 255);
$basename = iconv('UTF-8', 'ASCII//TRANSLIT', $displayname);
$basename = strtolower($basename);
$basename = preg_replace('/[^a-z]/', '', $basename);
if (strlen($basename) < 2) {
$basename = 'inst' . $basename;
}
else {
$basename = substr($basename, 0, 255);
}
// Make sure the name is unique. If it is not, add a suffix and see if
// that makes it unique
$finalname = $basename;
$suffix = 'a';
while (record_exists('institution', 'name', $finalname)) {
// Add the suffix but make sure the name length doesn't go over 255
$finalname = substr($basename, 0, 255 - strlen($suffix)) . $suffix;
// Will iterate a-z, aa-az, ba-bz, etc.
// See: http://php.net/manual/en/language.operators.increment.php
$suffix++;
}
return $finalname;
}
/**
* Callback function to extract user ID from an object.
* @param object $input
......
......@@ -4509,41 +4509,6 @@ function generate_urlid($dirty, $default, $minlength=3, $maxlength=100) {
return $s;
}
/**
* Generate a valid name for the institution.name column, based on the specified display name
*
* @param string $displayname
* @return string
*/
function generate_institution_name($displayname) {
// iconv can crash on strings that are too long, so truncate before converting
$basename = mb_substr($displayname, 0, 255);
$basename = iconv('UTF-8', 'ASCII//TRANSLIT', $displayname);
$basename = strtolower($basename);
$basename = preg_replace('/[^a-z]/', '', $basename);
if (strlen($basename) < 2) {
$basename = 'inst' . $basename;
}
else {
$basename = substr($basename, 0, 255);
}
// Make sure the name is unique. If it is not, add a suffix and see if
// that makes it unique
$finalname = $basename;
$suffix = 'a';
while (record_exists('institution', 'name', $finalname)) {
// Add the suffix but make sure the name length doesn't go over 255
$finalname = substr($basename, 0, 255 - strlen($suffix)) . $suffix;
// Will iterate a-z, aa-az, ba-bz, etc.
// See: http://php.net/manual/en/language.operators.increment.php
$suffix++;
}
return $finalname;
}
/**
* Sorts an array by one of the value fields
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment