Commit 2540cb8d authored by Théo GUILLON's avatar Théo GUILLON
Browse files

Correction : enregistrement des champs crées en bdd

Correction : des data envoyées au serveur
parent 9cbb84be
......@@ -470,7 +470,7 @@ class FormulairesController extends AppController {
$this->redirect($this->Referers->get());
}
debug($this->request->data);die;
// debug($this->request->data);die;
$success = true;
$this->Formulaire->begin();
......@@ -495,79 +495,22 @@ class FormulairesController extends AppController {
}
if ($success == true) {
$array = json_decode($data['Formulaire']['json'], true);
foreach ($array as $value) {
$sortie = array();
foreach ($value as $clef => $valeur) {
switch ($clef) {
case 'onglet':
$ongletChampCoresponsable = false;
$ongletChampSoustraitant = false;
if ($valeur === 'form-container-coresponsable') {
$ongletChampCoresponsable = true;
}
if ($valeur === 'form-container-soustraitant') {
$ongletChampSoustraitant = true;
}
break;
case 'type':
$type = $valeur;
break;
case 'ligne':
$ligne = (int)round($valeur, 0);
break;
case 'colonne':
$colonne = (int)round($valeur, 0);
break;
case 'name':
$sortie[$clef] = LettercaseFormattableBehavior::formatageVariable($valeur);
break;
case 'options':
$options = [];
foreach ($valeur as $key => $option) {
$options[$key] = trim($option);
}
$sortie[$clef] = $options;
break;
case 'default':
if (!is_array($valeur)) {
$sortie[$clef] = trim($valeur);
} else {
$options = [];
foreach ($valeur as $key => $option) {
$options[$key] = trim($option);
}
$sortie[$clef] = $options;
}
break;
default:
$sortie[$clef] = $valeur;
break;
}
if (isset($data['Formulaire']['form-container-formulaire'])) {
foreach ($data['Formulaire']['form-container-formulaire'] as $key => $fields) {
$success = $this->saveFields($id, $fields);
}
}
$this->Champ->create([
'formulaire_id' => $id,
'type' => $type,
'ligne' => $ligne,
'colonne' => $colonne,
'details' => json_encode($sortie),
'champ_coresponsable' => $ongletChampCoresponsable,
'champ_soustraitant' => $ongletChampSoustraitant,
]);
$success = false !== $this->Champ->save() && $success;
if (isset($data['Formulaire']['form-container-coresponsable']) && $success == true) {
foreach ($data['Formulaire']['form-container-coresponsable'] as $key => $fields) {
$success = $this->saveFields($id, $fields, true);
}
}
if (isset($data['Formulaire']['form-container-soustraitant']) && $success == true) {
foreach ($data['Formulaire']['form-container-soustraitant'] as $key => $fields) {
$success = $this->saveFields($id, $fields, false, true);
}
}
}
......@@ -590,6 +533,76 @@ class FormulairesController extends AppController {
$this->view = 'edit';
}
private function saveFields($formulaire_id, $fields, $ongletChampCoresponsable = false, $ongletChampSoustraitant = false)
{
$success = true;
foreach ($fields as $field) {
$array = (array)json_decode($field['details'], true);
$valeur = array_merge($field, $array);
unset($valeur['details']);
foreach ($valeur as $clef => $val) {
switch ($clef) {
case 'type':
$type = $val;
break;
case 'ligne':
$ligne = (int)round($val, 0);
break;
case 'colonne':
$colonne = (int)round($val, 0);
break;
case 'name':
$sortie[$clef] = LettercaseFormattableBehavior::formatageVariable($val);
break;
case 'options':
$options = [];
foreach ($val as $key => $option) {
$options[$key] = trim($option);
}
$sortie[$clef] = $options;
break;
case 'default':
if (!is_array($val)) {
$sortie[$clef] = trim($val);
} else {
$options = [];
foreach ($val as $key => $option) {
$options[$key] = trim($option);
}
$sortie[$clef] = $options;
}
break;
default:
$sortie[$clef] = $val;
break;
}
}
$this->Champ->create([
'formulaire_id' => $formulaire_id,
'type' => $type,
'ligne' => $ligne,
'colonne' => $colonne,
'details' => json_encode($sortie),
'champ_coresponsable' => $ongletChampCoresponsable,
'champ_soustraitant' => $ongletChampSoustraitant,
]);
$success = false !== $this->Champ->save(null, ['atomic' => false]) && $success;
}
return $success;
}
/**
* Récupére en BDD les champs défini dans le formulaire en fonction des différents onglet
* Ajoute les valeurs par défault défini dans le formulaire
......@@ -671,7 +684,7 @@ class FormulairesController extends AppController {
];
if (empty($this->request->data)) {
if ($formulaireOLD === false && $this->request->params['action'] == 'edit') {
if ($formulaireOLD === false) {
$this->request->data = $this->Formulaire->find('first', [
'conditions' => [
'id' => $formulaire_id
......@@ -700,49 +713,49 @@ class FormulairesController extends AppController {
}
}
}
}
// valeur par défault
// if ($this->request->params['action'] == 'add' && empty($this->request->data)) {
// $fields['coresponsable'] = [
// [
// 'Champ' => [
// 'type' => 'input',
// 'ligne' => 1,
// 'colonne' => 1,
// 'details' => '{"name":"nom_responsable_conjoint","placeholder":"Nom et prénom","label":"Nom et prénom du responsable conjoint","default":"","obligatoire":true,"repeat":false}'
// ]
// ],
// [
// 'Champ' => [
// 'type' => 'input',
// 'ligne' => 3,
// 'colonne' => 1,
// 'details' => '{"name":"structure_responsable_conjointe","placeholder":"Raison sociale","label":"Raison sociale de la structure conjointe","default":"","obligatoire":true,"repeat":false}'
// valeur par défault
// if ($this->request->params['action'] == 'add' && empty($this->request->data)) {
// $fields['coresponsable'] = [
// [
// 'Champ' => [
// 'type' => 'input',
// 'ligne' => 1,
// 'colonne' => 1,
// 'details' => '{"name":"nom_responsable_conjoint","placeholder":"Nom et prénom","label":"Nom et prénom du responsable conjoint","default":"","obligatoire":true,"repeat":false}'
// ]
// ],
// [
// 'Champ' => [
// 'type' => 'input',
// 'ligne' => 3,
// 'colonne' => 1,
// 'details' => '{"name":"structure_responsable_conjointe","placeholder":"Raison sociale","label":"Raison sociale de la structure conjointe","default":"","obligatoire":true,"repeat":false}'
// ]
// ],
// [
// 'Champ' => [
// 'type' => 'input',
// 'ligne' => 1,
// 'colonne' => 2,
// 'details' => '{"name":"email_responsable_conjoint","placeholder":"Adresse e-mail","label":"Adresse mail du responsable conjoint","default":"","obligatoire":true,"repeat":false}'
// ]
// ],
// [
// 'Champ' => [
// 'type' => 'textarea',
// 'ligne' => 3,
// 'colonne' => 2,
// 'details' => '{"name":"adresse_structure_responsable_conjointe","placeholder":"Adresse postale","label":"Adresse de la structure conjointe","default":"","obligatoire":false,"repeat":false}'
// ]
// ]
// ],
// [
// 'Champ' => [
// 'type' => 'input',
// 'ligne' => 1,
// 'colonne' => 2,
// 'details' => '{"name":"email_responsable_conjoint","placeholder":"Adresse e-mail","label":"Adresse mail du responsable conjoint","default":"","obligatoire":true,"repeat":false}'
// ]
// ],
// [
// 'Champ' => [
// 'type' => 'textarea',
// 'ligne' => 3,
// 'colonne' => 2,
// 'details' => '{"name":"adresse_structure_responsable_conjointe","placeholder":"Adresse postale","label":"Adresse de la structure conjointe","default":"","obligatoire":false,"repeat":false}'
// ]
// ]
// ];
// }
// ];
// }
$this->request->data['Formulaire']['form-container-formulaire'] = $fields['formulaire'];
$this->request->data['Formulaire']['form-container-coresponsable'] = $fields['coresponsable'];
$this->request->data['Formulaire']['form-container-soustraitant'] = $fields['soustraitant'];
$this->request->data['Formulaire']['form-container-formulaire'] = $fields['formulaire'];
$this->request->data['Formulaire']['form-container-coresponsable'] = $fields['coresponsable'];
$this->request->data['Formulaire']['form-container-soustraitant'] = $fields['soustraitant'];
}
}
......
......@@ -134,10 +134,6 @@ echo $this->WebcilForm->create('Formulaire', [
</div>
<div class="row">
<?php
echo $this->WebcilForm->hidden('', ['id' => 'hiddenForm']);
?>
<!-- Groupe bouton -->
<div class="col-md-12 top17 text-center">
<div class="btn-group">
......
......@@ -135,7 +135,7 @@ echo $this->WebcilForm->create('Formulaire', [
<div id="form-container-formulaire" class="form-container col-md-12">
<?php
echo $this->element('Formulaires/champDuFormulaire', [
'champs' => $fields['formulaire']
'champs' => $this->request->data['Formulaire']['form-container-formulaire']
]);
?>
</div>
......@@ -154,7 +154,7 @@ echo $this->WebcilForm->create('Formulaire', [
<div id="form-container-coresponsable" class="form-container col-md-12">
<?php
echo $this->element('Formulaires/champDuFormulaire', [
'champs' => $fields['coresponsable']
'champs' => $this->request->data['Formulaire']['form-container-coresponsable']
]);
?>
</div>
......@@ -176,7 +176,7 @@ echo $this->WebcilForm->create('Formulaire', [
<div id="form-container-soustraitant" class="form-container col-md-12">
<?php
echo $this->element('Formulaires/champDuFormulaire', [
'champs' => $fields['soustraitant']
'champs' => $this->request->data['Formulaire']['form-container-soustraitant']
]);
?>
</div>
......
......@@ -2,8 +2,7 @@ $(document).ready(function () {
$('#container').on('submit', function() {
var success = true,
tabRetour = {};
retour = {};
dynamicFields = [];
$('.ui-droppable').each(function(idxDroppable, droppable) {
var droppableId = $(droppable).attr('id'),
......@@ -13,17 +12,15 @@ $(document).ready(function () {
$(idTab).addClass('active');
$(droppable).find('.draggable').each(function (idxDraggable, draggable) {
var objFieldDetails = {};
var objFieldValues = {};
var fieldLine = null,
fieldColumn = null,
fieldType = null,
fieldName = null,
fieldPlaceholder = null,
fieldLabel = null,
fieldDefault = null,
fieldContent = null
fieldOptions = null,
fieldObligatoire = null,
fieldRepeat = null;
fieldName = null;
var options = [];
fieldLine = Math.round($(draggable).position().top / 35 + 1);
......@@ -42,10 +39,11 @@ $(document).ready(function () {
}
fieldType = 'input';
fieldName = $(draggable).find('input').attr('name');
fieldPlaceholder = $(draggable).find('input').attr('placeholder');
fieldLabel = $(draggable).find('.labeler').html();
fieldDefault = $(draggable).find('input').val();
objFieldDetails['name'] = $(draggable).find('input').attr('name');
objFieldDetails['placeholder'] = $(draggable).find('input').attr('placeholder');
objFieldDetails['label'] = $(draggable).find('.labeler').html();
objFieldDetails['default'] = $(draggable).find('input').val();
} else if ($(draggable).hasClass('long-text')) {
if (typeof $(draggable).find('textarea').attr('name') === '' || typeof $(draggable).find('textarea').attr('name') === 'undefined') {
......@@ -55,10 +53,12 @@ $(document).ready(function () {
}
fieldType = 'textarea';
fieldName = $(draggable).find('textarea').attr('name');
fieldPlaceholder = $(draggable).find('textarea').attr('placeholder');
fieldLabel = $(draggable).find('.labeler').html();
fieldDefault = $(draggable).find('textarea').val();
objFieldDetails['name'] = $(draggable).find('textarea').attr('name');
objFieldDetails['placeholder'] = $(draggable).find('textarea').attr('placeholder');
objFieldDetails['label'] = $(draggable).find('.labeler').html();
objFieldDetails['default'] = $(draggable).find('textarea').val();
} else if ($(draggable).hasClass('date')) {
if (typeof $(draggable).find('input').attr('name') === '' || typeof $(draggable).find('input').attr('name') === 'undefined') {
alert('Le nom de variable du champ date est incorrect');
......@@ -67,22 +67,23 @@ $(document).ready(function () {
}
fieldType = 'date';
fieldName = $(draggable).find('input').attr('name');
fieldPlaceholder = $(draggable).find('input').attr('placeholder');
fieldLabel = $(draggable).find('.labeler').html();
fieldDefault = $(draggable).find('input').val();
objFieldDetails['name'] = $(draggable).find('input').attr('name');
objFieldDetails['placeholder'] = $(draggable).find('input').attr('placeholder');
objFieldDetails['label'] = $(draggable).find('.labeler').html();
objFieldDetails['default'] = $(draggable).find('input').val();
} else if ($(draggable).hasClass('title')) {
fieldType = 'title';
fieldContent = $(draggable).find('h1').html();
objFieldDetails['content'] = $(draggable).find('h1').html();
} else if ($(draggable).hasClass('texte')) {
fieldType = 'texte';
fieldContent = $(draggable).find('h5').html();
objFieldDetails['content'] = $(draggable).find('h5').html();
} else if ($(draggable).hasClass('help')) {
fieldType = 'help';
fieldContent = $(draggable).find('.messager').html();
objFieldDetails['content'] = $(draggable).find('.messager').html();
} else if ($(draggable).hasClass('checkboxes')) {
if (typeof $(draggable).find('input').attr('name') === '' || typeof $(draggable).find('input').attr('name') === 'undefined') {
......@@ -92,20 +93,21 @@ $(document).ready(function () {
}
fieldType = 'checkboxes';
fieldName = $(draggable).find('input').attr('name');
fieldLabel = $(draggable).find('.labeler').html();
var options = [];
objFieldDetails['name'] = $(draggable).find('input').attr('name');
objFieldDetails['label'] = $(draggable).find('.labeler').html();
options = [];
$(draggable).find('input').each(function () {
options.push($(this).attr('value'));
});
fieldOptions = options;
objFieldDetails['options'] = options;
var valuesCheckboxesDefault = [];
$('input[name="'+fieldName+'"]:checked').each(function() {
valuesCheckboxesDefault.push(this.id);
});
fieldDefault = valuesCheckboxesDefault;
objFieldDetails['default'] = valuesCheckboxesDefault;
} else if ($(draggable).hasClass('radios')) {
if (typeof $(draggable).find('input').attr('name') === '' || typeof $(draggable).find('input').attr('name') === 'undefined') {
......@@ -115,15 +117,16 @@ $(document).ready(function () {
}
fieldType = 'radios';
fieldName = $(draggable).find('input').attr('name');
fieldLabel = $(draggable).find('.labeler').html();
fieldDefault = $(draggable).find('input[name="'+fieldName+'"]:checked').val();
var options = [];
objFieldDetails['name'] = $(draggable).find('input').attr('name');
objFieldDetails['label'] = $(draggable).find('.labeler').html();
objFieldDetails['default'] = $(draggable).find('input[name="'+fieldName+'"]:checked').val();
options = [];
$(draggable).find('input').each(function () {
options.push($(this).attr('value'));
});
fieldOptions = options;
objFieldDetails['options'] = options;
} else if ($(draggable).hasClass('deroulant')) {
if (typeof $(draggable).find('option').attr('name') === '' || typeof $(draggable).find('option').attr('name') === 'undefined') {
......@@ -133,16 +136,17 @@ $(document).ready(function () {
}
fieldType = 'deroulant';
fieldName = $(draggable).find('option').attr('name');
fieldLabel = $(draggable).find('.labeler').html();
var options = [];
objFieldDetails['name'] = $(draggable).find('option').attr('name');
objFieldDetails['label'] = $(draggable).find('.labeler').html();
options = [];
$(draggable).find('option').each(function () {
if ( this.value != '') {
options.push(this.value);
}
});
fieldOptions = options;
objFieldDetails['options'] = options;
var valuesDeroulantDefault = '';
$('option[name="'+fieldName+'"]:checked').each(function() {
......@@ -150,7 +154,7 @@ $(document).ready(function () {
valuesDeroulantDefault = this.id;
}
});
fieldDefault = valuesDeroulantDefault;
objFieldDetails['default'] = valuesDeroulantDefault;
} else if ($(draggable).hasClass('multi-select')) {
if (typeof $(draggable).find('option').attr('name') === '' || typeof $(draggable).find('option').attr('name') === 'undefined') {
......@@ -160,57 +164,47 @@ $(document).ready(function () {
}
fieldType = 'multi-select';
fieldName = $(draggable).find('option').attr('name');
fieldLabel = $(draggable).find('.labeler').html();
var options = [];
objFieldDetails['name'] = $(draggable).find('option').attr('name');
objFieldDetails['label'] = $(draggable).find('.labeler').html();
options = [];
$(draggable).find('option').each(function () {
options.push($(this).attr('value'));
});
fieldOptions = options;
objFieldDetails['options'] = options;
var valuesMultiSelectDefault = [];
$('option[name="'+fieldName+'"]:checked').each(function() {
valuesMultiSelectDefault.push(this.index);
});
fieldDefault = valuesMultiSelectDefault;
objFieldDetails['default'] = valuesMultiSelectDefault;
}
if ($(draggable).attr('data-virtual-required') === 'fieldRequired') {
fieldObligatoire = true;
objFieldDetails['obligatoire'] = true;
} else {
fieldObligatoire = false;
objFieldDetails['obligatoire'] = false;
}
if ($(draggable).attr('virtual-data-repeat') === 'fieldRepeat') {
fieldRepeat = true;
objFieldDetails['repeat'] = true;
} else {
fieldRepeat = false;
objFieldDetails['repeat'] = false;
}
var fieldDetails = {
'name': fieldName,
'placeholder': fieldPlaceholder,
'label': fieldLabel,
'default': fieldDefault,
'content' : fieldContent,
'options' : fieldOptions,
'obligatoire' : fieldObligatoire,
'repeat' : fieldRepeat
};
var fieldValues = {
objFieldValues = {
'type' : fieldType,
'ligne' : fieldLine,
'colonne' : fieldColumn,
'details' : JSON.stringify(fieldDetails)
'details' : JSON.stringify(objFieldDetails)
};
var tmpArray = {};
tmpArray['Champ'] = fieldValues;
tabRetour[idxDraggable] = tmpArray;
retour[droppableId] = tabRetour;
$.each(objFieldValues, function(fieldName, value) {
var hidden = $('<input type="hidden" name="data[Formulaire]['+droppableId+']['+idxDraggable+'][Champ]['+fieldName+']" value="" />');
hidden.attr('value', value);
dynamicFields.push(hidden);
});
});
});
......@@ -218,9 +212,11 @@ $(document).ready(function () {
$('#info_complementaire').addClass('active');
$('#onglet_info_complementaire').addClass('active');
var ret = JSON.stringify(retour, null, '\t');
if (success) {
$('#hiddenForm').attr('value', ret);
$.each(dynamicFields, function(idx, hidden) {
console.log(hidden);
$('form').append(hidden);
});
return true;