Commit 90c98db4 authored by Théo GUILLON's avatar Théo GUILLON
Browse files

Ajout : des champs supplementraire venant du formulaire sur chaque sous-traitant

En cour : ajout vérification obligatoire sur les champs supplémentaire de la co-responsabilité et de la sous-traitance
parent 835a9e83
......@@ -271,6 +271,7 @@ class FichesController extends AppController
'formulaire_id' => $formulaire_id
],
'fields' => [
'type',
'details',
'champ_coresponsable',
'champ_soustraitant'
......@@ -630,8 +631,6 @@ class FichesController extends AppController
$this->redirect($this->Referers->get());
}
debug($this->request->data);die;
$success = true;
$this->Fiche->begin();
......@@ -654,6 +653,7 @@ class FichesController extends AppController
'formulaire_id' => $form_id['Fiche']['form_id']
],
'fields' => [
'type',
'details',
'champ_coresponsable',
'champ_soustraitant'
......
......@@ -364,6 +364,7 @@ class WebdpoFiche extends AppModel {
// Soustraitance
$success = false !== $this->soustraitanceSave($Fiche->id, $data['Soustraitance'], $data['Fiche']['soustraitance']) && $success;
$data = $this->cleanAndCheckDataSoustraitance($data, $champs);
$newDataChamps = $this->checkDataConditons($data, $champs);
$data = Hash::extract($newDataChamps, 'data');
......@@ -927,6 +928,68 @@ class WebdpoFiche extends AppModel {
return $success;
}
private function cleanAndCheckDataSoustraitance($data, $champs)
{
if ($data['Fiche']['soustraitance'] == false) {
if (isset($data[$this->alias]['soustraitancefields'])) {
unset($data[$this->alias]['soustraitancefields']);
}
return $data;
}
$arraySoustraitanceFields = [];
foreach ($champs as $champ) {
if ($champ['Champ']['champ_soustraitant'] === true && $champ['Champ']['champ_coresponsable'] === false) {
$arraySoustraitanceFields[] = $champ;
}
}
// On vérifie que les champs suplémentaite du formualaire ne soient pas obligatoire
if (isset($data[$this->alias]['soustraitancefields']) && !empty($data[$this->alias]['soustraitancefields'])) {
$success = true;
foreach ($data[$this->alias]['soustraitancefields'] as $soustraitant_id => $soustraitancefield) {
foreach ($arraySoustraitanceFields as $arraySoustraitanceField) {
debug($arraySoustraitanceField);
$details = json_decode($arraySoustraitanceField['Champ']['details'], true);
debug($details);
if ($details['obligatoire'] === true) {
if (isset($this->validate['soustraitancefields'][$soustraitant_id][$details['name']]) == false) {
$this->validate['soustraitancefields'][$soustraitant_id][$details['name']] = [];
}
debug($this->validate);
debug('soustraitancefields');
debug($soustraitant_id);
debug($details['name']);
if (in_array($arraySoustraitanceField['Champ']['type'], ['checkboxes', 'multi-select'])) {
$this->validate['soustraitancefields'][$soustraitant_id][$details['name']]['multiple'] = ['rule' => ['multiple', ['min' => 1]]];
} else {
$this->validate['soustraitancefields'][$soustraitant_id][$details['name']]['notBlank'] = ['rule' => ['notBlank']];
}
debug($this->validate);
//@TODO ERREUR champ obligatoire
// $this->alias->invalidate('coresponsabilitefields['+$soustraitant_id+']['+$arraySoustraitanceField['name']+'', 'Champ obligatoire');
// $this->alias->validationErrors['coresponsabilitefields'][$soustraitant_id][$arraySoustraitanceField['name']] = 'Champ obligatoire';
$success = false;
}
}
}
if ($success === false) {
return $data;
}
$data[$this->alias]['soustraitancefields'] = json_encode($data[$this->alias]['soustraitancefields'], true);
}
return $data;
}
public function beforeValidate($options = array()) {
// $this->data = $this->cleanupData($this->data);
return parent::beforeValidate($options);
......
......@@ -38,22 +38,23 @@
}
?>
<div id="coresponsableCards">
<?php
if (!empty($coresponsables)) {
foreach ($coresponsables as $key => $coresponsable) {
echo $this->Form->hidden("Coresponsable.currentCoresponsable.{$coresponsable['Coresponsable']['responsable_id']}.responsable_id", [
'value' => $coresponsable['Coresponsable']['responsable_id'],
'id' => false
]);
echo $this->Form->hidden("Coresponsable.currentCoresponsable.{$coresponsable['Coresponsable']['responsable_id']}.coresponsable_id", [
'value' => $coresponsable['Coresponsable']['id'],
'id' => false
]);
}
<div id="coresponsableCards"></div>
<?php
if (!empty($coresponsables)) {
foreach ($coresponsables as $key => $coresponsable) {
echo $this->Form->hidden("Coresponsable.currentCoresponsable.{$coresponsable['Coresponsable']['responsable_id']}.responsable_id", [
'value' => $coresponsable['Coresponsable']['responsable_id'],
'id' => false
]);
echo $this->Form->hidden("Coresponsable.currentCoresponsable.{$coresponsable['Coresponsable']['responsable_id']}.coresponsable_id", [
'value' => $coresponsable['Coresponsable']['id'],
'id' => false
]);
}
?>
</div>
}
?>
</div>
<!-- Fin onglet Co-responsable -->
......@@ -210,8 +211,15 @@ if (isset($this->request->data['WebdpoFiche']['coresponsabilitefields'])) {
// On attribut la valeur au champ si il y en a une.
if (coresponsabilitefieldsRequestData !== null) {
console.log(coresponsabilitefieldsRequestData);
$.each(coresponsabilitefieldsRequestData[idSelect], function (key, val) {
$('#cr_'+idSelect+'_'+key).val(val);
if (jQuery.isArray(val)) {
$.each(val, function (k, v) {
$('#' + key + v).attr("checked", "checked");
});
} else {
$('#cr_' + idSelect + '_' + key).val(val);
}
});
}
}
......
......@@ -36,86 +36,40 @@
}
?>
<div id="soustraitanceCards">
<?php
if (!empty($soustraitances)) {
foreach ($soustraitances as $key => $soustraitance) {
echo $this->Form->hidden("Soustraitance.currentSoustraitance.{$soustraitance['Soustraitance']['soustraitant_id']}.soustraitant_id", [
'value' => $soustraitance['Soustraitance']['soustraitant_id'],
'id' => false
]);
echo $this->Form->hidden("Soustraitance.currentSoustraitance.{$soustraitance['Soustraitance']['soustraitant_id']}.soustraitance_id", [
'value' => $soustraitance['Soustraitance']['id'],
'id' => false
]);
<div id="soustraitanceCards"></div>
?>
<div id="card_soustraitance_<?php echo $soustraitance['Soustraitance']['soustraitant_id'];?>" class="row">
<div class="col-md-12">
<div class="panel panel-default ">
<div class="panel-body">
<div class="col-md-6">
<h4>
<?php echo $soustraitance['Soustraitance']['raisonsociale'];?>
</h4>
<p>
<u>N° SIRET :</u>
<?php echo $soustraitance['Soustraitance']['siret'];?>
</p>
<p>
<u>Code APE :</u>
<?php echo $soustraitance['Soustraitance']['ape'];?>
</p>
<?php
if (!empty($soustraitance['Soustraitance']['adresse'])) {
?>
<p>
<u>Adresse :</u>
<?php echo $soustraitance['Soustraitance']['adresse'];?>
</p>
<?php
}
if (!empty($soustraitance['Soustraitance']['telephone'])) {
?>
<p>
<u>Téléphone :</u>
<?php echo $soustraitance['Soustraitance']['telephone'];?>
</p>
<?php
}
if (!empty($soustraitance['Soustraitance']['fax'])) {
?>
<p>
<u>Fax :</u>
<?php echo $soustraitance['Soustraitance']['fax'];?>
</p>
<?php
}
if (!empty($soustraitance['Soustraitance']['email'])) {
?>
<p>
<u>E-mail :</u>
<?php echo $soustraitance['Soustraitance']['email'];?>
</p>
<?php
}
?>
</div>
</div>
</div>
</div>
</div>
<?php
}
<?php
if (!empty($soustraitances)) {
foreach ($soustraitances as $key => $soustraitance) {
echo $this->Form->hidden("Soustraitance.currentSoustraitance.{$soustraitance['Soustraitance']['soustraitant_id']}.soustraitant_id", [
'value' => $soustraitance['Soustraitance']['soustraitant_id'],
'id' => false
]);
echo $this->Form->hidden("Soustraitance.currentSoustraitance.{$soustraitance['Soustraitance']['soustraitant_id']}.soustraitance_id", [
'value' => $soustraitance['Soustraitance']['id'],
'id' => false
]);
}
?>
</div>
}
?>
</div>
<!-- Fin onglet Soustraitance -->
<?php
if (isset($this->request->data['Soustraitance']['soustraitances'])) {
$soustraitancesRequestData = $this->request->data['Soustraitance']['soustraitances'];
} else {
$soustraitancesRequestData = null;
}
if (isset($this->request->data['WebdpoFiche']['soustraitancefields'])) {
$soustraitancefieldsRequestData = $this->request->data['WebdpoFiche']['soustraitancefields'];
} else {
$soustraitancefieldsRequestData = null;
}
?>
<script type="text/javascript">
$(document).ready(function () {
......@@ -127,77 +81,34 @@
let infoSoustraitances = <?php echo json_encode(Hash::combine($soustraitants, '{n}.Soustraitant.id', '{n}.Soustraitant'))?>;
$(eventSelect).on("select2:select", function (e) {
let idSelect = e.params.data.id;
let soustraitant = infoSoustraitances[idSelect];
let colLeft = '';
<?php
// if (isset($fields['coresponsable'])) {
// $fields = $this->element('Fiches/tabs/formulaire', [
// 'champs' => $fields['coresponsable']
// ]);
// }
?>
//let fields = <?php //echo json_encode($fields);?>//;
let formCoreponsableFields = '';
//if (fields) {
// formCoreponsableFields = '<div id="form-container-coresponsable">';
// formCoreponsableFields = formCoreponsableFields + fields;
// formCoreponsableFields = formCoreponsableFields + '</div>';
//}
if (soustraitant['adresse'] != null) {
colLeft = colLeft + '<p>' +
'<u>Adresse :</u>' +
soustraitant['adresse'] +
'</p>';
}
if (soustraitant['telephone'] != null) {
colLeft = colLeft + '<p>' +
'<u>Téléphone :</u>' +
soustraitant['telephone'] +
'</p>';
<?php
if (isset($fields['soustraitant']) && !empty($fields['soustraitant'])) {
$templateFieldsSoustraitance = $this->element('Fiches/tabs/formulaire', [
'champs' => $fields['soustraitant']
]);
$arrayFieldsId = [];
foreach ($fields['soustraitant'] as $field) {
$arrayFieldsId[] = json_decode($field['Champ']['details'], true)['name'];
}
} else {
$arrayFieldsId = [];
$templateFieldsSoustraitance = '';
}
?>
if (soustraitant['fax'] != null) {
colLeft = colLeft + '<p>' +
'<u>Fax :</u>' +
soustraitant['fax'] +
'</p>';
}
let templateFieldsSoustraitance = <?php echo json_encode($templateFieldsSoustraitance);?>;
let soustraitancesRequestData = <?php echo json_encode($soustraitancesRequestData);?>;
let soustraitancefieldsRequestData = <?php echo json_encode($soustraitancefieldsRequestData);?>;
if (soustraitant['email'] != null) {
colLeft = colLeft + '<p>' +
'<u>E-mail :</u>' +
soustraitant['email'] +
'</p>';
}
if (soustraitancesRequestData) {
$(soustraitancesRequestData).each(function (key, val) {
createCardSoustraitant(templateFieldsSoustraitance, infoSoustraitances, val, soustraitancefieldsRequestData);
});
}
$('#soustraitanceCards').append('<div id="card_soustraitance_'+idSelect+'" class="row">' +
'<div class="col-md-12">' +
' <div class="panel panel-default ">' +
' <div class="panel-body">' +
' <div class="col-md-6">' +
' <h4>' + soustraitant['raisonsociale'] + '</h4>' +
' <p>' +
' <u>N° SIRET :</u>' +
soustraitant['siret'] +
' </p>' +
' <p>' +
' <u>Code APE :</u>' +
soustraitant['ape'] +
' </p>' +
colLeft +
' </div>' +
formCoreponsableFields +
' </div>' +
' </div>' +
'</div>' +
'</div>');
$(eventSelect).on("select2:select", function (e) {
let idSelect = e.params.data.id;
createCardSoustraitant(templateFieldsSoustraitance, infoSoustraitances, idSelect);
});
$(eventSelect).on("select2:unselect", function (e) {
......@@ -209,7 +120,92 @@
$('input[type="hidden"][name="data[Soustraitance][currentSoustraitance]['+id+'][soustraitant_id]"]').remove();
$('input[type="hidden"][name="data[Soustraitance][currentSoustraitance]['+id+'][soustraitance_id]"]').remove();
});
});
function createCardSoustraitant(templateFieldsSoustraitance, infoSoustraitances, idSelect, soustraitancefieldsRequestData = null) {
let soustraitant = infoSoustraitances[idSelect];
let colLeft = '';
let formSoustraitanceFields = '';
if (templateFieldsSoustraitance) {
let fields = templateFieldsSoustraitance.replace(/data\[WebdpoFiche\]/g, 'data[WebdpoFiche][soustraitancefields]['+idSelect+']');
fields = fields.replace(/(for|id)="(<?php echo implode('|', $arrayFieldsId);?>)"/g, '$1="st_'+idSelect+'_$2"');
if (fields) {
formSoustraitanceFields = '<div id="form-container-soustraitance">';
formSoustraitanceFields = formSoustraitanceFields + fields;
formSoustraitanceFields = formSoustraitanceFields + '</div>';
}
}
if (soustraitant['adresse'] != null) {
colLeft = colLeft + '<p>' +
'<u>Adresse :</u>' +
soustraitant['adresse'] +
'</p>';
}
if (soustraitant['telephone'] != null) {
colLeft = colLeft + '<p>' +
'<u>Téléphone :</u>' +
soustraitant['telephone'] +
'</p>';
}
if (soustraitant['fax'] != null) {
colLeft = colLeft + '<p>' +
'<u>Fax :</u>' +
soustraitant['fax'] +
'</p>';
}
if (soustraitant['email'] != null) {
colLeft = colLeft + '<p>' +
'<u>E-mail :</u>' +
soustraitant['email'] +
'</p>';
}
$('#soustraitanceCards').append('<div id="card_soustraitance_'+idSelect+'" class="row">' +
'<div class="col-md-12">' +
' <div class="panel panel-default ">' +
' <div class="panel-body">' +
' <div class="col-md-12">' +
' <div class="col-md-6">' +
' <h4>' + soustraitant['raisonsociale'] + '</h4>' +
' <p>' +
' <u>N° SIRET :</u>' +
soustraitant['siret'] +
' </p>' +
' <p>' +
' <u>Code APE :</u>' +
soustraitant['ape'] +
' </p>' +
' </div>' +
' <div class="col-md-6">' +
colLeft +
' </div>' +
' </div>' +
formSoustraitanceFields +
' </div>' +
' </div>' +
'</div>' +
'</div>');
// On attribut la valeur au champ si il y en a une.
if (soustraitancefieldsRequestData !== null) {
console.log(soustraitancefieldsRequestData);
$.each(soustraitancefieldsRequestData[idSelect], function (key, val) {
console.log(val);
if (jQuery.isArray(val)) {
$.each(val, function (k, v) {
$('#' + key + v).attr("checked", "checked");
});
} else {
$('#st_'+idSelect+'_'+key).val(val);
}
});
}
}
</script>
\ No newline at end of file
<?php
echo $this->Html->script('jquery-mask-plugin/dist/jquery.mask.min.js');
debug($this->request->data);
debug($this->validationErrors);
if (isset($this->validationErrors['WebdpoFiche']) && !empty($this->validationErrors['WebdpoFiche'])) {
?>
......
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