Commit 3d3aa3cd authored by Théo GUILLON's avatar Théo GUILLON
Browse files

Modification : enregistrement des champs concernant la condition uniquement.

Ajout : vérification que la condition ne soit pas en doublons sur le champ
parent 057a8d11
......@@ -537,15 +537,41 @@ class FormulairesController extends AppController {
{
$success = true;
$typeFields = [
'input',
'textarea',
'date',
'checkboxes',
'radios',
'deroulant',
'multi-select',
'title',
'help',
'texte',
];
$valueConditionResult = [
'shown',
'hidden',
];
foreach ($fields as $field) {
$array = (array)json_decode($field['details'], true);
$valeur = array_merge($field, $array);
unset($valeur['details']);
foreach ($valeur as $clef => $val) {
if ($success == false) {
break;
}
switch ($clef) {
case 'type':
$type = $val;
if (in_array($val, $typeFields)) {
$type = $val;
} else {
$success = false;
}
break;
case 'ligne':
......@@ -583,12 +609,66 @@ class FormulairesController extends AppController {
break;
case 'conditions':
$sortie[$clef] = $val;
$conditionsChecked = [];
$conditions = json_decode($val, true);
debug($conditions);
foreach ($conditions as $uuid => $condition) {
$condition = (array)$condition;
// $conditions = (array)json_decode($val);
// foreach ($conditions as $key => $condition) {
// $sortie[$clef][$key] = $condition;
// }
if (!isset($condition['ifTheField']) || $condition['ifTheField'] == '') {
$success = false;
}
if (!isset($condition['hasValue']) || $condition['hasValue'] == '') {
$success = false;
}
if (!isset($condition['thenTheField']) || $condition['thenTheField'] == '') {
$success = false;
}
if (!isset($condition['mustBe']) || in_array($condition['mustBe'], $valueConditionResult) == false) {
$success = false;
}
if (!isset($condition['ifNot']) || in_array($condition['ifNot'], $valueConditionResult) == false) {
$success = false;
}
if ($success == true) {
$save = true;
// On recupère uniquement les champs qui nous interesse
$tmpCondition = [
'ifTheField' => $condition['ifTheField'],
'hasValue' => $condition['hasValue'],
'thenTheField' => $condition['thenTheField'],
'mustBe' => $condition['mustBe'],
'ifNot' => $condition['ifNot'],
];
if (!empty($conditionsChecked)) {
// On vérifie dans les conditions près à être sauvegarder qu'il n'y a pas de doublon
foreach ($conditionsChecked as $tmp) {
if ($save == false) {
break;
}
$diff_result = array_diff($tmpCondition, $tmp);
if (empty($diff_result)) {
$save = false;
}
}
}
if ($save == true) {
$conditionsChecked[$uuid] = $tmpCondition;
}
}
}
$sortie[$clef] = json_encode($conditionsChecked, JSON_FORCE_OBJECT);
break;
default:
......@@ -597,7 +677,10 @@ class FormulairesController extends AppController {
}
}
$this->Champ->create([
if ($success == false) {
break;
}
$data = [
'formulaire_id' => $formulaire_id,
'type' => $type,
'ligne' => $ligne,
......@@ -605,7 +688,8 @@ class FormulairesController extends AppController {
'details' => json_encode($sortie),
'champ_coresponsable' => $ongletChampCoresponsable,
'champ_soustraitant' => $ongletChampSoustraitant,
]);
];
$this->Champ->create($data);
$success = false !== $this->Champ->save(null, ['atomic' => false]) && $success;
}
......
Markdown is supported
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