Commit 21b3aa5f authored by Théo GUILLON's avatar Théo GUILLON
Browse files

Ajout : mise en place modele virtuel WebdpoCoresponsable

Ajout : mise en place modele virtuel WebdpoSoustraitance
Modification : affichage erreur
Correction : taille deroulant-multi formulaire
parent 90c98db4
......@@ -2196,4 +2196,22 @@ class FichesController extends AppController
return $typages;
}
public function test()
{
if (empty($this->request->data) === false) {
$webdpoFiche = ClassRegistry::init('WebdpoFiche');
$webdpoFiche->invalidate('outilnom', 'Erreur outilnom');
debug($this->request->data);
$webdpoSoustraitance = ClassRegistry::init('WebdpoSoustraitance');
$webdpoSoustraitance->validate = ['coucou' => ['notBlank' => ['rule' => ['notBlank']]]];
debug(in_array(false, $webdpoSoustraitance->saveAll($this->request->data['WebdpoSoustraitance'], ['atomic' => false, 'validate' => 'only'])) !== true);//@fixme
debug($webdpoSoustraitance->validationErrors);
/*for($i=0;$i<=1;$i++){
$webdpoSoustraitance->invalidate("coucou['.$i.']", 'Erreur coucou');
}*/
}
}
}
<?php
App::uses('AppModel', 'Model');
class WebdpoCoresponsable extends AppModel
{
public $name = 'WebdpoCoresponsable';
public $useTable = false;
protected $_schema = [];
public function coresponsableSave ($fiche_id, $dataCoresponsable, $useCoresponsable)
{
$success = true;
$Coresponsable = ClassRegistry::init('Coresponsable');
if (empty($dataCoresponsable['currentCoresponsable'])
&& empty($dataCoresponsable['coresponsables'])
&& $useCoresponsable == true
) {
$Coresponsable->invalidate('coresponsables', 'Champ obligatoire');
return false;
}
if (empty($fiche_id)) {
return false;
}
if ($useCoresponsable == false) {
$success = $Coresponsable->deleteAll([
'Coresponsable.fiche_id' => $fiche_id
]) && $success;
return true;
}
if (isset($dataCoresponsable['currentCoresponsable']) && !empty($dataCoresponsable['currentCoresponsable']) && !empty($dataCoresponsable['coresponsables'])) {
foreach ($dataCoresponsable['currentCoresponsable'] as $currentCoresponsable) {
if (($key = array_search($currentCoresponsable['responsable_id'], $dataCoresponsable['coresponsables'])) !== false) {
unset($dataCoresponsable['coresponsables'][$key]);
}
}
$noDeleteCoresponsable = Hash::extract($dataCoresponsable['currentCoresponsable'], '{n}.coresponsable_id');
$success = $Coresponsable->deleteAll([
'Coresponsable.id !=' => $noDeleteCoresponsable,
'Coresponsable.fiche_id' => $fiche_id
]) && $success;
} else {
$success = $Coresponsable->deleteAll([
'Coresponsable.fiche_id' => $fiche_id
]) && $success;
}
if ($success === false) {
return $success;
}
if (!empty($dataCoresponsable['coresponsables'])) {
$Responsable = ClassRegistry::init('Responsable');
foreach ($dataCoresponsable['coresponsables'] as $coresponsable_id) {
$responsable = $Responsable->find('first', [
'conditions' => [
'id' => $coresponsable_id
]
]);
$Coresponsable->create([
'fiche_id' => $fiche_id,
'responsable_id' => $responsable['Responsable']['id'],
'nomcoresponsable' => $responsable['Responsable']['nomresponsable'],
'prenomcoresponsable' => $responsable['Responsable']['prenomresponsable'],
'fonctioncoresponsable' => $responsable['Responsable']['fonctionresponsable'],
'emailcoresponsable' => $responsable['Responsable']['emailresponsable'],
'telephonecoresponsable' => $responsable['Responsable']['telephoneresponsable'],
'raisonsocialestructure' => $responsable['Responsable']['raisonsocialestructure'],
'siretstructure' => $responsable['Responsable']['siretstructure'],
'apestructure' => $responsable['Responsable']['apestructure'],
'telephonestructure' => $responsable['Responsable']['telephonestructure'],
'faxstructure' => $responsable['Responsable']['faxstructure'],
'adressestructure' => $responsable['Responsable']['adressestructure'],
'emailstructure' => $responsable['Responsable']['emailstructure']
]);
$success = false !== $Coresponsable->save(null, ['atomic' => false]) && $success;
}
}
return $success;
}
public function cleanAndCheckDataCoresponsable($data, $champs)
{
if ($data['Fiche']['coresponsable'] == false) {
if (isset($data['WebdpoFiche']['coresponsabilitefields'])) {
unset($data['WebdpoFiche']['coresponsabilitefields']);
}
if (isset($data['Coresponsable'])) {
unset($data['Coresponsable']);
}
if (isset($data[$this->alias])) {
unset($data[$this->alias]);
}
return $data;
}
$arrayCoresponsableFields = [];
foreach ($champs as $champ) {
if ($champ['Champ']['champ_coresponsable'] === true && $champ['Champ']['champ_soustraitant'] === false) {
$arrayCoresponsableFields[] = $champ;
}
}
// On vérifie que les champs suplémentaite du formualaire ne soient pas obligatoire
if (isset($data[$this->alias]) && !empty($data[$this->alias])) {
$success = true;
foreach ($data[$this->alias] as $responsable_id => $coresponsabilitefield) {
foreach ($arrayCoresponsableFields as $arrayCoresponsableField) {
$details = json_decode($arrayCoresponsableField['Champ']['details'], true);
if ($details['obligatoire'] === true) {
if (in_array($arrayCoresponsableField['Champ']['type'], ['checkboxes', 'multi-select'])) {
if (empty($coresponsabilitefield[$details['name']])) {
$this->validate[$details['name']]['multiple'] = ['rule' => ['multiple', ['min' => 1]]];
$success = false;
}
} else {
if (empty($coresponsabilitefield[$details['name']])) {
$this->validate[$details['name']]['notBlank'] = ['rule' => ['notBlank']];
$success = false;
}
}
$this->saveAll($data[$this->alias], ['atomic' => false, 'validate' => 'only']);
}
}
}
if ($success === false) {
return $data;
}
$data[$this->alias]['coresponsabilitefields'] = json_encode($data[$this->alias], true);
}
return $data;
}
}
\ No newline at end of file
......@@ -359,12 +359,14 @@ class WebdpoFiche extends AppModel {
$success = false !== $Fiche->save(null, ['atomic' => false]) && $success;
// Co-responsable
$success = false !== $this->coresponsableSave($Fiche->id, $data['Coresponsable'], $data['Fiche']['coresponsable']) && $success;
$data = $this->cleanAndCheckDataCoresponsable($data, $champs);
$WebdpoCoresponsable = ClassRegistry::init('WebdpoCoresponsable');
$success = false !== $WebdpoCoresponsable->coresponsableSave($Fiche->id, $data['Coresponsable'], $data['Fiche']['coresponsable']) && $success;
$data = $WebdpoCoresponsable->cleanAndCheckDataCoresponsable($data, $champs);
// Soustraitance
$success = false !== $this->soustraitanceSave($Fiche->id, $data['Soustraitance'], $data['Fiche']['soustraitance']) && $success;
$data = $this->cleanAndCheckDataSoustraitance($data, $champs);
$WebdpoSoustraitance = ClassRegistry::init('WebdpoSoustraitance');
$success = false !== $WebdpoSoustraitance->soustraitanceSave($Fiche->id, $data['Soustraitance'], $data['Fiche']['soustraitance']) && $success;
$data = $WebdpoSoustraitance->cleanAndCheckDataSoustraitance($data, $champs);
$newDataChamps = $this->checkDataConditons($data, $champs);
$data = Hash::extract($newDataChamps, 'data');
......@@ -730,266 +732,6 @@ class WebdpoFiche extends AppModel {
return ($newDataChamps);
}
private function coresponsableSave ($fiche_id, $dataCoresponsable, $useCoresponsable)
{
$success = true;
$Coresponsable = ClassRegistry::init('Coresponsable');
if (empty($dataCoresponsable['currentCoresponsable'])
&& empty($dataCoresponsable['coresponsables'])
&& $useCoresponsable == true
) {
$Coresponsable->invalidate('coresponsables', 'Champ obligatoire');
return false;
}
if (empty($fiche_id)) {
return false;
}
if ($useCoresponsable == false) {
$success = $Coresponsable->deleteAll([
'Coresponsable.fiche_id' => $fiche_id
]) && $success;
return true;
}
if (isset($dataCoresponsable['currentCoresponsable']) && !empty($dataCoresponsable['currentCoresponsable']) && !empty($dataCoresponsable['coresponsables'])) {
foreach ($dataCoresponsable['currentCoresponsable'] as $currentCoresponsable) {
if (($key = array_search($currentCoresponsable['responsable_id'], $dataCoresponsable['coresponsables'])) !== false) {
unset($dataCoresponsable['coresponsables'][$key]);
}
}
$noDeleteCoresponsable = Hash::extract($dataCoresponsable['currentCoresponsable'], '{n}.coresponsable_id');
$success = $Coresponsable->deleteAll([
'Coresponsable.id !=' => $noDeleteCoresponsable,
'Coresponsable.fiche_id' => $fiche_id
]) && $success;
} else {
$success = $Coresponsable->deleteAll([
'Coresponsable.fiche_id' => $fiche_id
]) && $success;
}
if ($success === false) {
return $success;
}
if (!empty($dataCoresponsable['coresponsables'])) {
$Responsable = ClassRegistry::init('Responsable');
foreach ($dataCoresponsable['coresponsables'] as $coresponsable_id) {
$responsable = $Responsable->find('first', [
'conditions' => [
'id' => $coresponsable_id
]
]);
$Coresponsable->create([
'fiche_id' => $fiche_id,
'responsable_id' => $responsable['Responsable']['id'],
'nomcoresponsable' => $responsable['Responsable']['nomresponsable'],
'prenomcoresponsable' => $responsable['Responsable']['prenomresponsable'],
'fonctioncoresponsable' => $responsable['Responsable']['fonctionresponsable'],
'emailcoresponsable' => $responsable['Responsable']['emailresponsable'],
'telephonecoresponsable' => $responsable['Responsable']['telephoneresponsable'],
'raisonsocialestructure' => $responsable['Responsable']['raisonsocialestructure'],
'siretstructure' => $responsable['Responsable']['siretstructure'],
'apestructure' => $responsable['Responsable']['apestructure'],
'telephonestructure' => $responsable['Responsable']['telephonestructure'],
'faxstructure' => $responsable['Responsable']['faxstructure'],
'adressestructure' => $responsable['Responsable']['adressestructure'],
'emailstructure' => $responsable['Responsable']['emailstructure']
]);
$success = false !== $Coresponsable->save(null, ['atomic' => false]) && $success;
}
}
return $success;
}
private function cleanAndCheckDataCoresponsable($data, $champs)
{
if ($data['Fiche']['coresponsable'] == false) {
if (isset($data[$this->alias]['coresponsabilitefields'])) {
unset($data[$this->alias]['coresponsabilitefields']);
}
return $data;
}
$arrayCoresponsableFields = [];
foreach ($champs as $champ) {
if ($champ['Champ']['champ_coresponsable'] === true && $champ['Champ']['champ_soustraitant'] === false) {
$arrayCoresponsableFields[] = json_decode($champ['Champ']['details'], true);
}
}
// On vérifie que les champs suplémentaite du formualaire ne soient pas obligatoire
if (isset($data[$this->alias]['coresponsabilitefields']) && !empty($data[$this->alias]['coresponsabilitefields'])) {
$success = true;
foreach ($data[$this->alias]['coresponsabilitefields'] as $responsable_id => $coresponsabilitefield) {
foreach ($arrayCoresponsableFields as $arrayCoresponsableField) {
if (empty($coresponsabilitefield[$arrayCoresponsableField['name']]) && $arrayCoresponsableField['obligatoire'] === true) {
//@TODO ERREUR champ obligatoire
// $this->alias->validationErrors['coresponsabilitefields'][$responsable_id][$arrayCoresponsableField['name']] = 'Champ obligatoire';
$success = false;
}
}
}
if ($success === false) {
return $data;
}
$data[$this->alias]['coresponsabilitefields'] = json_encode($data[$this->alias]['coresponsabilitefields'], true);
}
return $data;
}
private function soustraitanceSave ($fiche_id, $dataSoustraitance, $useSoustraitance)
{
$success = true;
$Soustraitance = ClassRegistry::init('Soustraitance');
if (empty($dataSoustraitance['currentSoustraitance'])
&& empty($dataSoustraitance['soustraitances'])
&& $useSoustraitance == true
) {
$Soustraitance->invalidate('soustraitances', 'Champ obligatoire');
return false;
}
if (empty($fiche_id)) {
return false;
}
if ($useSoustraitance == false) {
$success = $Soustraitance->deleteAll([
'Soustraitance.fiche_id' => $fiche_id
]) && $success;
return true;
}
if (isset($dataSoustraitance['currentSoustraitance']) && !empty($dataSoustraitance['currentSoustraitance']) && !empty($dataSoustraitance['soustraitances'])) {
foreach ($dataSoustraitance['currentSoustraitance'] as $currentSoustraitance) {
if (($key = array_search($currentSoustraitance['soustraitant_id'], $dataSoustraitance['soustraitances'])) !== false) {
unset($dataSoustraitance['soustraitances'][$key]);
}
}
$noDeleteSoustraitance = Hash::extract($dataSoustraitance['currentSoustraitance'], '{n}.soustraitance_id');
$success = $Soustraitance->deleteAll([
'Soustraitance.id !=' => $noDeleteSoustraitance,
'Soustraitance.fiche_id' => $fiche_id
]) && $success;
} else {
$success = $Soustraitance->deleteAll([
'Soustraitance.fiche_id' => $fiche_id
]) && $success;
}
if ($success === false) {
return $success;
}
if (!empty($dataSoustraitance['soustraitances'])) {
$Soustraitant = ClassRegistry::init('Soustraitant');
foreach ($dataSoustraitance['soustraitances'] as $soustraitant_id) {
$soustraitant = $Soustraitant->find('first', [
'conditions' => [
'id' => $soustraitant_id
]
]);
$Soustraitance->create([
'fiche_id' => $fiche_id,
'soustraitant_id' => $soustraitant['Soustraitant']['id'],
'raisonsociale' => $soustraitant['Soustraitant']['raisonsociale'],
'siret' => $soustraitant['Soustraitant']['siret'],
'ape' => $soustraitant['Soustraitant']['ape'],
'telephone' => $soustraitant['Soustraitant']['telephone'],
'fax' => $soustraitant['Soustraitant']['fax'],
'adresse' => $soustraitant['Soustraitant']['adresse'],
'email' => $soustraitant['Soustraitant']['email'],
]);
$success = false !== $Soustraitance->save(null, ['atomic' => false]) && $success;
}
}
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);
......
<?php
App::uses('AppModel', 'Model');
class WebdpoSoustraitance extends AppModel
{
public $name = 'WebdpoSoustraitance';
public $useTable = false;
protected $_schema = [];
/**
* Enregistrement en bdd du ou des soutritants selectionner
*
* @param $fiche_id
* @param $dataSoustraitance
* @param $useSoustraitance
* @return bool
* @throws Exception
*/
public function soustraitanceSave ($fiche_id, $dataSoustraitance, $useSoustraitance)
{
$success = true;
$Soustraitance = ClassRegistry::init('Soustraitance');
if (empty($dataSoustraitance['currentSoustraitance'])
&& empty($dataSoustraitance['soustraitances'])
&& $useSoustraitance == true
) {
$Soustraitance->invalidate('soustraitances', 'Champ obligatoire');
return false;
}
if (empty($fiche_id)) {
return false;
}
if ($useSoustraitance == false) {
$success = $Soustraitance->deleteAll([
'Soustraitance.fiche_id' => $fiche_id
]) && $success;
return true;
}
if (isset($dataSoustraitance['currentSoustraitance']) && !empty($dataSoustraitance['currentSoustraitance']) && !empty($dataSoustraitance['soustraitances'])) {
foreach ($dataSoustraitance['currentSoustraitance'] as $currentSoustraitance) {
if (($key = array_search($currentSoustraitance['soustraitant_id'], $dataSoustraitance['soustraitances'])) !== false) {
unset($dataSoustraitance['soustraitances'][$key]);
}
}
$noDeleteSoustraitance = Hash::extract($dataSoustraitance['currentSoustraitance'], '{n}.soustraitance_id');
$success = $Soustraitance->deleteAll([
'Soustraitance.id !=' => $noDeleteSoustraitance,
'Soustraitance.fiche_id' => $fiche_id
]) && $success;
} else {
$success = $Soustraitance->deleteAll([
'Soustraitance.fiche_id' => $fiche_id
]) && $success;
}
if ($success === false) {
return $success;
}
if (!empty($dataSoustraitance['soustraitances'])) {
$Soustraitant = ClassRegistry::init('Soustraitant');
foreach ($dataSoustraitance['soustraitances'] as $soustraitant_id) {
$soustraitant = $Soustraitant->find('first', [
'conditions' => [
'id' => $soustraitant_id
]
]);
$Soustraitance->create([
'fiche_id' => $fiche_id,
'soustraitant_id' => $soustraitant['Soustraitant']['id'],
'raisonsociale' => $soustraitant['Soustraitant']['raisonsociale'],
'siret' => $soustraitant['Soustraitant']['siret'],
'ape' => $soustraitant['Soustraitant']['ape'],
'telephone' => $soustraitant['Soustraitant']['telephone'],
'fax' => $soustraitant['Soustraitant']['fax'],
'adresse' => $soustraitant['Soustraitant']['adresse'],
'email' => $soustraitant['Soustraitant']['email'],
]);
$success = false !== $Soustraitance->save(null, ['atomic' => false]) && $success;
}
}
return $success;
}
public function cleanAndCheckDataSoustraitance($data, $champs)
{
if ($data['Fiche']['soustraitance'] == false) {
if (isset($data['WebdpoFiche']['soustraitancefields'])) {
unset($data['WebdpoFiche']['soustraitancefields']);
}
if (isset($data['Soustraitance'])) {
unset($data['Soustraitance']);
}
if (isset($data[$this->alias])) {
unset($data[$this->alias]);
}
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]) && !empty($data[$this->alias])) {
$success = true;
foreach ($data[$this->alias] as $soustraitant_id => $soustraitancefield) {
foreach ($arraySoustraitanceFields as $arraySoustraitanceField) {
$details = json_decode($arraySoustraitanceField['Champ']['details'], true);
if ($details['obligatoire'] === true) {
if (in_array($arraySoustraitanceField['Champ']['type'], ['checkboxes', 'multi-select'])) {
if (empty($soustraitancefield[$details['name']])) {