Commit 6f40bd83 authored by Théo GUILLON's avatar Théo GUILLON
Browse files

Modification : des conditions en jquery

Ajout : vérification des conditions coté serveur
parent 45e38c2e
......@@ -480,6 +480,10 @@ class WebdpoFiche extends AppModel {
$Fiche->create($data['Fiche']);
$success = false !== $Fiche->save(null, ['atomic' => false]) && $success;
$newDataChamps = $this->checkDataConditons($data, $champs);
$data = Hash::extract($newDataChamps, 'data');
$champs = Hash::extract($newDataChamps, 'champs');
foreach ($champs as $champ) {
$details = json_decode(Hash::get($champ, 'Champ.details'));
if ($details->obligatoire == true) {
......@@ -785,6 +789,80 @@ class WebdpoFiche extends AppModel {
return $success;
}
/**
* On vérifie que les champs dans "data" soit en adéquation avec les conditions créées sur les champs
*
* @access protected
*
* @param $data
* @param $champs
* @return array
*
* @created 15/04/2020
* @version V1.2.0
* @author Théo GUILLON <theo.guillon@libriciel.coop>
*/
protected function checkDataConditons($data, $champs)
{
foreach ($champs as $key => $champ) {
$options = json_decode($champ['Champ']['details'], true);
if (isset($options['conditions']) && !empty($options['conditions'])) {
$fieldConditions = json_decode($options['conditions'], true);
foreach ($fieldConditions as $fieldCondition) {
if (isset($fieldCondition['ifTheField']) && isset($data[$this->alias][$fieldCondition['ifTheField']])) {
$valueCondition = '';
if (is_array($data[$this->alias][$fieldCondition['ifTheField']])) {
// champ checkbox, multi-select
if (in_array(substr($fieldCondition['hasValue'], -1), $data[$this->alias][$fieldCondition['ifTheField']])){
$valueCondition = $fieldCondition['mustBe'];
} else {
$valueCondition = $fieldCondition['ifNot'];
}
} else {
// input, textearea, radio, deroulant
if ($data[$this->alias][$fieldCondition['ifTheField']] == $fieldCondition['hasValue']) {
$valueCondition = $fieldCondition['mustBe'];
} else {
$valueCondition = $fieldCondition['ifNot'];
}
}
if ($valueCondition == 'hidden' && isset($data[$this->alias][$fieldCondition['thenTheField']])) {
// On supprimer le champ qui doit être cacher par la condition
unset($data[$this->alias][$fieldCondition['thenTheField']]);
if ($options['obligatoire'] == true) {
$options['obligatoire'] = false;
$champs[$key]['Champ']['details'] = json_encode($options);
}
}
} else {
// On supprimer le champ qui doit être cacher par la condition
unset($data[$this->alias][$fieldCondition['thenTheField']]);
if ($options['obligatoire'] == true) {
$options['obligatoire'] = false;
$champs[$key]['Champ']['details'] = json_encode($options);
}
}
}
}
}
$newDataChamps = [
'data' => $data,
'champs' => $champs
];
return ($newDataChamps);
}
public function beforeValidate($options = array()) {
// $this->data = $this->cleanupData($this->data);
return parent::beforeValidate($options);
......
......@@ -232,9 +232,9 @@ $line = 1;
*
* @see Search.prototype.resizeSearch
*/
var fixSelect2SearchFieldWidth = function() {
let fixSelect2SearchFieldWidth = function() {
$('.select2.select2-container').each(function(idx, container) {
var field = $(container).find('.select2-search__field');
let field = $(container).find('.select2-search__field');
if (field && $(field).attr('placeholder') !== '') {
$(field).width($(container).width());
}
......@@ -245,11 +245,11 @@ $line = 1;
* Lorsque la classe d'un tab-pane change, on exécute la fonction fixSelect2SearchFieldWidth
* @see https://stackoverflow.com/a/19401707
*/
// var $div = $("#info_formulaire");
var observer = new MutationObserver(function(mutations) {
// let $div = $("#info_formulaire");
let observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.attributeName === "class") {
var attributeValue = $(mutation.target).prop(mutation.attributeName);
let attributeValue = $(mutation.target).prop(mutation.attributeName);
fixSelect2SearchFieldWidth();
}
});
......@@ -260,7 +260,7 @@ $line = 1;
});
//--------------------------------------------------------------------------------------------------------------
var champsDate = null;
let champsDate = null;
champsDate = <?php echo json_encode($calendrier); ?>;
jQuery.each(champsDate, function(key, val){
$('#' + val).datetimepicker({
......@@ -273,21 +273,61 @@ $line = 1;
});
// Mise en place des conditions
var conditions = <?php echo json_encode($conditions); ?>;
let conditions = <?php echo json_encode($conditions); ?>;
$.each(conditions, function (key, value) {
// On cache le champ par défault
shownHideField(value['ifNot'], value['thenTheField']);
// shownHideField(value['ifNot'], value['thenTheField']);
// Si le champ a comme balise "select" (deroulant et multi dera)
// Si le champ a comme balise "select" (deroulant et multi-select)
if ($('#'+value['ifTheField']).is('select')) {
if ($('#'+value['ifTheField']).val() == value['hasValue']) {
shownHideField(value['mustBe'], value['thenTheField'])
if ($('#'+value['ifTheField']).hasClass('multiSelect') == true) {
$('#'+value['ifTheField']).parent().parent().on('show', function() {
if (jQuery.inArray(value['hasValue'], $('#'+value['ifTheField']).val()) !== -1) {
shownHideField(value['mustBe'], value['thenTheField'])
} else {
shownHideField(value['ifNot'], value['thenTheField']);
}
});
$('#'+value['ifTheField']).parent().parent().on('hide', function() {
if (jQuery.inArray(value['hasValue'], $('#'+value['ifTheField']).val()) !== -1) {
shownHideField(value['mustBe'], value['thenTheField'])
} else {
shownHideField(value['ifNot'], value['thenTheField']);
}
});
if (jQuery.inArray(value['hasValue'], $('#'+value['ifTheField']).val()) !== -1 && $('#'+value['ifTheField']).parent().parent().css('display') != 'none') {
shownHideField(value['mustBe'], value['thenTheField'])
} else {
shownHideField(value['ifNot'], value['thenTheField']);
}
} else {
shownHideField(value['ifNot'], value['thenTheField']);
$('#'+value['ifTheField']).parent().parent().on('show', function() {
if ($('#'+value['ifTheField']).val() == value['hasValue']) {
shownHideField(value['mustBe'], value['thenTheField'])
} else {
shownHideField(value['ifNot'], value['thenTheField']);
}
});
$('#'+value['ifTheField']).parent().parent().on('hide', function() {
if ($('#'+value['ifTheField']).val() == value['hasValue']) {
shownHideField(value['mustBe'], value['thenTheField'])
} else {
shownHideField(value['ifNot'], value['thenTheField']);
}
});
if ($('#'+value['ifTheField']).val() == value['hasValue'] && $('#'+value['ifTheField']).parent().parent().css('display') != 'none') {
shownHideField(value['mustBe'], value['thenTheField'])
} else {
shownHideField(value['ifNot'], value['thenTheField']);
}
}
// Quand le champ deroulant change de valeur
$('#' + value['ifTheField']).change(function () {
// Quand le champ deroulant ou multi-select change de valeur
$('#'+value['ifTheField']).change(function () {
if ($('#'+value['ifTheField']).hasClass('multiSelect') == true) {
if (jQuery.inArray(value['hasValue'], $(this).val()) !== -1) {
shownHideField(value['mustBe'], value['thenTheField'])
......@@ -309,7 +349,19 @@ $line = 1;
// Si le champ est de type "checkbox"
if ($('#'+value['hasValue']).attr('type') == "checkbox") {
if ($('#'+value['hasValue']).prop("checked") == true) {
$('#'+value['hasValue']).parent().parent().parent().on('show', function() {
if ($('#'+value['hasValue']).prop("checked") == true) {
shownHideField(value['mustBe'], value['thenTheField'])
} else {
shownHideField(value['ifNot'], value['thenTheField']);
}
});
$('#'+value['hasValue']).parent().parent().parent().on('hide', function() {
shownHideField(value['ifNot'], value['thenTheField']);
});
if ($('#'+value['hasValue']).prop("checked") === true && $('#'+value['ifTheField']).parent().parent().css('display') != 'none') {
shownHideField(value['mustBe'], value['thenTheField'])
} else {
shownHideField(value['ifNot'], value['thenTheField']);
......@@ -327,8 +379,34 @@ $line = 1;
// Si le champ est de type "radio"
if ($('#'+value['hasValue']).attr('type') == "radio") {
$('#'+value['ifTheField']).parent().parent().on('show', function() {
$('#'+value['ifTheField']).parent().find('input[type=radio]:checked').each(function () {
if ($(this).val() == $('#'+value['hasValue']).attr('value')) {
shownHideField(value['mustBe'], value['thenTheField'])
} else {
shownHideField(value['ifNot'], value['thenTheField']);
}
});
});
$('#'+value['ifTheField']).parent().parent().on('hide', function() {
shownHideField(value['ifNot'], value['thenTheField']);
});
if ($('#'+value['ifTheField']).parent().parent().css('display') != 'none') {
if ($('#'+value['ifTheField']).parent().find('input[type=radio]:checked').val() == $('#'+value['hasValue']).attr('value')) {
shownHideField(value['mustBe'], value['thenTheField'])
} else {
shownHideField(value['ifNot'], value['thenTheField']);
}
} else {
shownHideField(value['ifNot'], value['thenTheField']);
}
// Quand le champ radio change de valeur
$('input[type=radio]').on('change', function () {
$('#'+value['ifTheField']).parent().find('input[type=radio]').on('change', function () {
if ($(this).val() == $('#'+value['hasValue']).attr('value')) {
shownHideField(value['mustBe'], value['thenTheField'])
} else {
......@@ -341,7 +419,7 @@ $line = 1;
function shownHideField(condition, thenTheField)
{
if (condition == 'shown') {
if (condition === 'shown') {
$('#' + thenTheField).parent().parent().show();
} else {
$('#' + thenTheField).parent().parent().hide();
......@@ -349,4 +427,14 @@ $line = 1;
}
}
});
(function ($) {
$.each(['show', 'hide'], function (i, ev) {
let el = $.fn[ev];
$.fn[ev] = function () {
this.trigger(ev);
return el.apply(this, arguments);
};
});
})(jQuery);
</script>
\ No newline at end of file
......@@ -1240,6 +1240,7 @@ var createForm = function(typeCreateForm) {
$(idContainer).find('.ui-selected').find('input[type="hidden"]').attr('id', newNameField+'_');
$(idContainer).find('.ui-selected').find('input[type="hidden"]').attr('name', newNameField);
$(idContainer).find('.ui-selected').find('select').attr('name', newNameField);
$(idContainer).find('.ui-selected').find('select').attr('id', newNameField);
// give to field the options
$(idContainer).find('.ui-selected').find('.contentMultiSelect').html(jQuery(objet));
......
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