Unverified Commit 47885b1d authored by Benjamin Rousselière's avatar Benjamin Rousselière Committed by Jerome Boucher
Browse files

feat/14478 : add parameters constraints

parent b389d645
......@@ -82,6 +82,9 @@ class ArchiveTransfer extends abstractMessage
}
$schema = $connectorConf['schema'];
$confParams = $connectorConf['params'];
$params = $this->checkParamsConstraints($confParams, $params);
$message = $this->createNewMessage($schema);
......@@ -105,6 +108,56 @@ class ArchiveTransfer extends abstractMessage
return $this->sendAcknowledgement($message);
}
protected function checkParamsConstraints($confParams, $params)
{
$this->translator->setCatalog("medona/messages");
foreach ($params as $name => $param) {
if (!isset($confParams[$name])) {
$this->sendError("404", "The sent parameters don't match the configuration parameters");
break;
}
$confParam = $confParams[$name];
if (!isset($confParam["type"])) {
$confParam["type"] = "text";
}
switch ($confParam["type"]) {
case 'number':
if (!is_numeric($param)) {
$this->sendError("405", $this->translator->getText("The parameter") . " '$name' " . $this->translator->getText("needs to be a number"));
}
break;
case 'boolean':
if ($param !== true && $param !== false) {
$this->sendError("405", $this->translator->getText("The parameter") . " '$name' " . $this->translator->getText("needs to be a boolean"));
}
break;
case 'enum':
if (!in_array($param, $confParam["enumNames"])) {
$this->sendError("405", $this->translator->getText("The parameter") . " '$name' " . $this->translator->getText("is not in the list"));
}
break;
case 'organization':
$this->orgController->getOrgByRegNumber($param);
break;
case 'archivalProfile':
$this->archivalProfileController->getByReference($param);
break;
}
if ($this->errors == 0 && isset($confParam["default"]) && $confParam["type"] != "file" && $param == '') {
$params[$name] = $confParam["default"];
}
}
if (count($this->errors) > 0) {
$exception = \laabs::newException('medona/invalidMessageException', "Invalid message", 400);
$exception->errors = $this->errors;
throw $exception;
}
return $params;
}
protected function createNewMessage($schema = null)
{
$messageId = \laabs::newId();
......
......@@ -43,6 +43,8 @@ class message
protected $archivalAgreementController;
protected $archivalProfileController;
protected $sdoFactory;
protected $messageTypeController;
......@@ -81,11 +83,13 @@ class message
protected $packageSchemas = [];
protected $packageConnectors = [];
protected $translator;
/**
* Constructor
* @param string $messageDirectory The message directory
* @param \dependency\sdo\Factory $sdoFactory The dependency Sdo Factory
* @param \dependency\localisation\TranslatorInterface $translator The localisation dependency
* @param bool $parentsDerogation See the child org messages
* @param array $removeMessageTask Tasks to remove medona messages directories
* @param integer $autoValidateSize Min size for auto-validation
......@@ -94,6 +98,7 @@ class message
public function __construct(
$messageDirectory,
\dependency\sdo\Factory $sdoFactory,
\dependency\localisation\TranslatorInterface $translator,
$parentsDerogation = true,
$removeMessageTask = null,
$autoValidateSize = 0,
......@@ -107,6 +112,7 @@ class message
$this->lifeCycleJournalController = \laabs::newController('lifeCycle/journal');
$this->digitalResourceController = \laabs::newController('digitalResource/digitalResource');
$this->archivalAgreementController = \laabs::newController('medona/archivalAgreement');
$this->archivalProfileController = \laabs::newController('recordsManagement/archivalProfile');
$this->profilesDirectory = \laabs::configuration('recordsManagement')['profilesDirectory'];
......@@ -130,6 +136,7 @@ class message
$this->packageSchemas = $packageSchemas;
$this->packageConnectors = $packageConnectors;
$this->translator = $translator;
}
/**
......
......@@ -64,6 +64,7 @@ trait archiveTransferTrait
public function getSourceInputs($schema, $source)
{
$inputs = [];
$notTextTypes = ['file', 'boolean', 'number', 'enum'];
if (isset(\laabs::configuration('medona')['packageConnectors'][$source]['params'])) {
$sourceInputs = \laabs::configuration('medona')['packageConnectors'][$source]['params'];
......@@ -71,7 +72,11 @@ trait archiveTransferTrait
if (isset($input["source"]) && $input["source"] == 'input') {
$input['name'] = $key;
if (!isset($input['type'])) {
$input['type'] = 'string';
$input['type'] = 'text';
}
$input['typeAccepted'] = false;
if (in_array($input['type'], $notTextTypes)) {
$input['typeAccepted'] = true;
}
$inputs[] = $input;
}
......
......@@ -76,6 +76,16 @@ class archivalProfile
return $this->view->saveHtml();
}
/**
* Get archival profiles list
*
* @return string
*/
public function archivalProfileList($archivalProfiles)
{
return json_encode($archivalProfiles, JSON_UNESCAPED_UNICODE);
}
/**
* The view to create or edit a archival profile
* @param string $archivalProfile The archival profile identifier
......
......@@ -59,9 +59,6 @@ msgstr "Transferts en attente de traitement"
msgid "Delivery incoming list"
msgstr "Demandes de communication"
msgid "Delivery process list"
msgstr "Communications à finaliser"
msgid "Destruction incoming list"
msgstr "Demandes d'élimination"
......@@ -569,6 +566,21 @@ msgstr "Message invalide"
msgid "Name of zip and his content files doesn't match"
msgstr "Le nom du zip et de ses fichiers ne correspondent pas"
msgid "The sent parameters don't match the configuration parameters"
msgstr "Les paramètres envoyés ne correspondent pas avec les paramètres de la configuration"
msgid "The parameter"
msgstr "Le paramètre"
msgid "needs to be a number"
msgstr "doit être un nombre"
msgid "needs to be a boolean"
msgstr "doit être un booléen"
msgid "is not in the list"
msgstr "n'est pas dans la liste"
msgid "Malformed XML"
msgstr "Fichier XML malformé"
......
......@@ -357,9 +357,14 @@ var zipContent = "";
};
var sourceInputs = $('#userInputs').find('input');
var sourceSelects = $('#userInputs').find('select');
$.each(sourceInputs, function() {
var name = $(this).attr("name");
if (!name) {
return;
}
let value;
switch($(this).attr("type")) {
case 'file':
......@@ -374,7 +379,10 @@ var zipContent = "";
jsonObject.params[name] = serializeFile(file, reader);
}
break;
case 'checkbox':
value = $(this).is(":checked");
jsonObject.params[name] = value;
break;
case 'text':
default:
let value = $(this).val();
......@@ -386,6 +394,12 @@ var zipContent = "";
}
});
$.each(sourceSelects, function() {
let value = $(this).val();
let name = $(this).attr("name");
jsonObject.params[name] = value;
});
return jsonObject;
}
......
......@@ -17,12 +17,33 @@
<input type="text" class="form-control" name="[?merge .['name'] ?]" required="[?merge .['required'].bool().then('true', 'false') ?]" placeholder="[?merge .['default'] ?]" value="[?merge .['default'] ?]"/>
</div>
<?merge .['type'].ifeq('number') ?>
<div class="form-group col-sm-8">
<input type="number" class="form-control" name="[?merge .['name'] ?]" required="[?merge .['required'].bool().then('true', 'false') ?]" data-default="[?merge .['default'] ?]"/>
<?merge .['default'].bool() ?>
<span><span data-translate-catalog="medona/messages">Default</span> : <?merge .['default'] ?></span>
</div>
<?merge .['type'].ifeq('boolean') ?>
<div class="form-group col-sm-8">
<input type="checkbox" class="form-check-input" name="[?merge .['name'] ?]" required="[?merge .['required'].bool().then('true', 'false') ?]" data-default="[?merge .['default'] ?]"/>
</div>
<?merge .['type'].ifeq('enum') ?>
<div class="form-group col-sm-8">
<select class="form-control" name="[?merge .['name'] ?]" required="[?merge .['required'].bool().then('true', 'false') ?]" data-default="[?merge .['default'] ?]">
<?merge .['enumNames'] ?>
<option value="[?merge . ?]"><?merge . ?></option>
</select>
</div>
<?merge .['type'].ifeq('file') ?>
<div class="form-group col-sm-8">
<?merge .['multiple'] @multiple ?>
<input type="file" name="[?merge .['name'] ?]" class="fileBrowser btn btn-light" data-show-upload="true" data-show-caption="true">
</div>
</div>
<span id="noOriginatorFound" class="hide">No originator found</span>
</div>
<script>
......@@ -48,4 +69,99 @@
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
function getTypeahead(field) {
let fieldName = field.data("external");
var data = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace(),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/descriptionRef/'+fieldName+'?query=%QUERY',
wildcard: '%QUERY'
}
});
data.initialize();
field.typeahead(
{
hint: true,
highlight: true,
minLength: 1
},
{
name: 'data',
source: data.ttAdapter(),
templates: {
suggestion: function(data) {
delete data._query;
var html = $('<div>');
var keyname = Object.keys(data)[0];
var key = data.keyname;
html.append($('<strong class="badge">').text(key));
delete data[keyname];
var value = Object.keys(data).map(function(name){return data[name]}).join(" - ");
html.append(' '+value);
return html;
}
}
}
).on('typeahead:selected', function ($event, suggestion, source) {
var keys = Object.keys(suggestion); //fetched the key at first index
field.val(suggestion[keys[1]]);
field.typeahead('val', suggestion[keys[1]]);
});
return field;
}
function setTypeahead(url, key, name) {
var entities = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace(key),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {url: url, ttl: '0'},
limit: 100
});
entities.initialize();
// initialize typeahead
$('.' + name).typeahead(
{
hint: true,
highlight: true,
minLength: 1
},
{
name: name + 's',
displayKey: key,
templates: {
suggestion: function(entity) {
var display =
"<span>"
+ "<span style='font-family:Helvetica, sans-serif;'>";
display += entity[key] + "</span></span><br>";
return display;
}
},
source: function(query, cb) {
entities.search(query, function(suggestions) {
cb(suggestions);
});
},
skipCache: true
}
);
}
$("#inputsDiv").ready(() => {
$('[data-external]').each(function() {
let field = $(this);
getTypeahead(field);
});
window.localStorage.clear();
setTypeahead('/organizations/todisplay?orgUnit=true&ownerOrg=true', 'registrationNumber', 'organization');
setTypeahead('/archivalprofiles/todisplay', 'reference', 'archivalProfile');
})
</script>
......@@ -34,6 +34,14 @@ interface archivalProfileInterface
*/
public function readArchivalprofiles();
/**
* Get the archivalProfiles list
*
* @return recordsManagement/archivalProfile/archivalProfileList
* @uses recordsManagement/archivalProfile/readIndex
*/
public function readArchivalprofilesTodisplay();
/**
* New empty archival profile with default values
*
......
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