Commit 76fe802a authored by Christian BUFFIN's avatar Christian BUFFIN
Browse files

Corrections et améliorations du paramétrage des responsables (cf. issue 362)

parent d0c2e660
......@@ -26,122 +26,144 @@ class ResponsablesController extends AppController {
public $uses = [
'Responsable',
'ResponsableOrganisation',
'Organisation'
];
/**
* @throws ForbiddenException
*
* @author Théo GUILLON <theo.guillon@libriciel.coop>
* @access public
* @created 12/04/2018
* @version v1.0.0
* Vérification de l'accès aux actions en fonction du profil de l'utilisateur connecté.
* Les vérifications de l'accès aux enregistrements se font dans les méthodes d'actions.
*/
public function index() {
if (true !== $this->Droits->authorized(ListeDroit::GESTION_CORESPONSABLE)) {
throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage'));
}
$this->set('title', __d('responsable', 'responsable.titreGestionResponsableApplication'));
$condition = [];
public function beforeFilter() {
parent::beforeFilter();
$query = [
$this->Droits->assertAuthorized([ListeDroit::GESTION_CORESPONSABLE]);
}
/**
* Retourne les résultats du moteur de recherche, que ce soit pour la liste complète ou pour la liste de ceux
* présents dans l'entité.
*
* @return array
*/
protected function _getSearchResults() {
$paginate = [
'contain' => [
'Organisation' => [
'id',
'raisonsociale',
'order' => ['raisonsociale']
]
],
'conditions' => [],
'order' => [
'Responsable.raisonsocialestructure ASC'
]
];
// Applications des filtres
if ($this->request->is('post')) {
// Affectation
if (!empty($this->request->data['ResponsableOrganisation']['organisation_id'])) {
if (isset($this->request->data['ResponsableOrganisation']) === true) {
$success = true;
$this->Organisation->begin();
$organisations_ids = Hash::extract($this->request->data, 'ResponsableOrganisation.organisation_id');
$responsables_ids = Hash::extract($this->request->data, 'ResponsableOrganisation.responsable_id');
foreach ($organisations_ids as $organisation_id) {
$responsableEntite = [];
$responsableEntite = $this->ResponsableOrganisation->find('list', [
'conditions' => [
'organisation_id' => $organisation_id
],
'fields' => [
'responsable_id'
]
]);
$diff = array_diff($responsableEntite, $responsables_ids);
$resultResponsable = [];
if (!empty($diff)) {
$resultResponsable = array_merge($responsables_ids, $responsableEntite);
} else {
$resultResponsable = $responsables_ids;
}
$data = [
'Organisation' => [
'id' => $organisation_id,
],
'Responsable' => [
'Responsable' => $resultResponsable
]
];
$this->Organisation->create($data);
$success = $success && false !== $this->Organisation->save(null, ['atomic' => false]);
}
if ($success == true) {
$this->Organisation->commit();
$this->Session->setFlash(__d('responsable', 'responsable.flashsuccessSousTraitantAffecterEnregistrer'), 'flashsuccess');
if ($this->request->params['action'] === 'index') {
// Filtrer par entité associée
if (!empty($this->request->data['Filtre']['organisation'])) {
$paginate['conditions'][] = $this->Responsable->getConditionOrganisation($this->request->data['Filtre']['organisation']);
}
// Filtrer par entité créatrice
$createdbyorganisation = (string)Hash::get($this->request->data, 'Filtre.createdbyorganisation');
if ($createdbyorganisation !== '') {
$paginate['conditions'][] = ['Responsable.createdbyorganisation' => $createdbyorganisation];
}
}
$this->redirect(['action' => 'index']);
} else {
$this->Organisation->rollback();
$this->Session->setFlash(__d('responsable', 'responsable.flasherrorErreurEnregistrementSousTraitantAffecter'), 'flasherror');
}
$filters = [
// Filtrer par raison sociale (du responsable)
'Filtre.raisonsocialestructure' => 'Responsable.raisonsocialestructure',
// Filtrer par numéro SIRET (du responsable)
'Filtre.siretstructure' => 'Responsable.siretstructure',
// Filtrer par code APE (du responsable)
'Filtre.apestructure' => 'Responsable.apestructure',
];
foreach ($filters as $filter => $path) {
$value = (string)Hash::get($this->request->data, $filter);
if ($value !== '') {
$paginate['conditions'][$path] = $value;
}
}
}
// Filtre
if (isset($this->request->data['Filtre']) === true) {
$condition = $this->_findFiltre($condition);
// Ajout de conditions suivant l'utilisateur connecté et l'action
if ($this->request->params['action'] === 'entite') {
$paginate['conditions'][] = $this->Responsable->getConditionOrganisation($this->Session->read('Organisation.id'));
}
$query['conditions'] = $condition;
$this->paginate = $paginate;
return $this->paginate($this->Responsable);
}
// Filtre sur l'utilisateur à l'origine du traitement
if (!empty($this->request->data['Filtre']['organisation'])) {
$subQuery = [
'alias' => 'responsables_organisations',
/**
* @throws ForbiddenException
*
* @author Théo GUILLON <theo.guillon@libriciel.coop>
* @access public
* @created 12/04/2018
* @version v1.0.0
*/
public function index() {
$this->set('title', __d('responsable', 'responsable.titreGestionResponsableApplication'));
if ($this->request->is('post')) {
// Affectation
if (!empty($this->request->data['ResponsableOrganisation']['organisation_id']) && isset($this->request->data['ResponsableOrganisation']) === true) {
$success = true;
$this->Responsable->ResponsableOrganisation->Organisation->begin();
$organisations_ids = Hash::extract($this->request->data, 'ResponsableOrganisation.organisation_id');
$responsables_ids = Hash::extract($this->request->data, 'ResponsableOrganisation.responsable_id');
foreach ($organisations_ids as $organisation_id) {
$responsableEntite = $this->Responsable->ResponsableOrganisation->find('list', [
'conditions' => [
'organisation_id' => $organisation_id
],
'fields' => [
'responsables_organisations.responsable_id'
'responsable_id'
]
]);
$diff = array_diff($responsableEntite, $responsables_ids);
if (!empty($diff)) {
$resultResponsable = array_merge($responsables_ids, $responsableEntite);
} else {
$resultResponsable = $responsables_ids;
}
$data = [
'Organisation' => [
'id' => $organisation_id,
],
'conditions' => [
'responsables_organisations.organisation_id' => $this->request->data['Filtre']['organisation']
'Responsable' => [
'Responsable' => $resultResponsable
]
];
$sql = $this->Responsable->ResponsableOrganisation->sql($subQuery);
$query['conditions'][] = "Responsable.id IN ( {$sql} )";
$success = $success && false !== $this->Responsable->ResponsableOrganisation->Organisation->save($data, ['atomic' => false]);
}
if ($success === true) {
$this->Responsable->ResponsableOrganisation->Organisation->commit();
$this->Session->setFlash(__d('responsable', 'responsable.flashsuccessSousTraitantAffecterEnregistrer'), 'flashsuccess');
} else {
$this->Responsable->ResponsableOrganisation->Organisation->rollback();
$this->Session->setFlash(__d('responsable', 'responsable.flasherrorErreurEnregistrementSousTraitantAffecter'), 'flasherror');
}
}
}
$this->paginate = $query;
$responsables = $this->paginate($this->Responsable);
$this->set(compact('responsables'));
$this->set([
'mesOrganisations' => $this->_mesOrganisations(),
'options' => $this->_optionsFiltre(),
'responsables' => $this->_getSearchResults(),
]);
}
protected function _mesOrganisations() {
$mesOrganisations = $this->WebcilUsers->organisations(
'list',
[
......@@ -150,74 +172,21 @@ class ResponsablesController extends AppController {
: ListeDroit::MODIFIER_UTILISATEUR
]
);
$this->set(compact('mesOrganisations'));
$options = $this->_optionsFiltre();
$this->set(compact('options'));
return $mesOrganisations;
}
private function _optionsFiltre($organisation_id = null) {
$query = [];
$condition = [];
if ($organisation_id !== null) {
$query = [
'conditions' => $condition,
'order' => [
'Responsable.raisonsocialestructure ASC'
]
];;
$subQuery = [
'alias' => 'responsables_organisations',
'fields' => [
'responsables_organisations.responsable_id'
],
'conditions' => [
'responsables_organisations.organisation_id' => $organisation_id
]
];
$sql = $this->ResponsableOrganisation->sql($subQuery);
$query['conditions'][] = "Responsable.id IN ( {$sql} )";
}
$query['fields'] = 'raisonsocialestructure';
$raisonsociale = $this->Responsable->find('list', $query);
$query['fields'] = 'siretstructure';
$siret = $this->Responsable->find('list', $query);
$query['fields'] = 'apestructure';
$ape = $this->Responsable->find('list', $query);
protected function _optionsFiltre($organisation_id = null) {
$options = [
'raisonsocialestructure' => array_combine($raisonsociale, $raisonsociale),
'siretstructure' => array_combine($siret, $siret),
'apestructure' => array_combine($ape, $ape)
'apestructure' => $this->Responsable->getStringOptionList('apestructure', $organisation_id),
'organisations' => $this->Responsable->Organisation->find('list', ['order' => ['Organisation.raisonsociale ASC']]),
'raisonsocialestructure' => $this->Responsable->getStringOptionList('raisonsocialestructure', $organisation_id),
'siretstructure' => $this->Responsable->getStringOptionList('siretstructure', $organisation_id),
];
return ($options);
}
private function _findFiltre($condition) {
// Filtre sur le numéro de siret
if (!empty($this->request->data['Filtre']['siretstructure'])) {
$condition['Responsable.siretstructure'] = $this->request->data['Filtre']['siretstructure'];
}
// Filtre sur le code ape
if (!empty($this->request->data['Filtre']['apestructure'])) {
$condition['Responsable.apestructure'] = $this->request->data['Filtre']['apestructure'];
}
// Filtre sur la raison sociale
if (!empty($this->request->data['Filtre']['raisonsocialestructure'])) {
$condition['Responsable.raisonsocialestructure'] = $this->request->data['Filtre']['raisonsocialestructure'];
}
return ($condition);
return $options;
}
/**
*
* @throws ForbiddenException
......@@ -228,48 +197,12 @@ class ResponsablesController extends AppController {
* @version v1.0.0
*/
public function entite() {
if (true !== $this->Droits->authorized(ListeDroit::GESTION_CORESPONSABLE)) {
throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage'));
}
$this->set('title', __d('responsable', 'responsable.titreGestionResponsableEntitee'));
$organisation_id = $this->Session->read('Organisation.id');
$condition = [];
if ($this->request->is('post')) {
// Filtre
if (isset($this->request->data['Filtre']) === true) {
$condition = $this->_findFiltre($condition);
}
}
$query = [
'conditions' => $condition,
'order' => [
'Responsable.raisonsocialestructure ASC'
]
];
$subQuery = [
'alias' => 'responsables_organisations',
'fields' => [
'responsables_organisations.responsable_id'
],
'conditions' => [
'responsables_organisations.organisation_id' => $organisation_id
]
];
$sql = $this->ResponsableOrganisation->sql($subQuery);
$query['conditions'][] = "Responsable.id IN ( {$sql} )";
$this->paginate = $query;
$responsables = $this->paginate($this->Responsable);
$this->set(compact('responsables'));
$options = $this->_optionsFiltre($organisation_id);
$this->set(compact('options'));
$this->set([
'options' => $this->_optionsFiltre($this->Session->read('Organisation.id')),
'responsables' => $this->_getSearchResults(),
]);
}
/**
......@@ -283,57 +216,7 @@ class ResponsablesController extends AppController {
* @version V1.0.0
*/
public function add() {
if (true !== $this->Droits->authorized(ListeDroit::GESTION_CORESPONSABLE)) {
throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage'));
}
$this->set('title', __d('responsable', 'responsable.titreAjouterResponsable'));
if ($this->request->is('post')) {
if ('Cancel' === Hash::get($this->request->data, 'submit')) {
$this->redirect($this->Referers->get());
}
$success = true;
$this->Responsable->begin();
$this->Responsable->create($this->request->data['Responsable']);
$success = $success && false !== $this->Responsable->save();
if (!empty($this->request->data['ResponsableOrganisation']['organisation_id']) && $success == true) {
$responsableId = $this->Responsable->id;
foreach ($this->request->data['ResponsableOrganisation']['organisation_id'] as $organisationId) {
if ($success == true) {
$this->ResponsableOrganisation->create([
'responsable_id' => $responsableId,
'organisation_id' => $organisationId
]);
$success = $success && false !== $this->ResponsableOrganisation->save();
}
}
}
if ($success == true) {
$this->Responsable->commit();
$this->Session->setFlash(__d('role', 'role.flashsuccessProfilEnregistrer'), 'flashsuccess');
$this->redirect($this->Referers->get());
} else {
$this->Responsable->rollback();
$this->Session->setFlash(__d('role', 'role.flasherrorErreurEnregistrementProfil'), 'flasherror');
}
}
$mesOrganisations = $this->WebcilUsers->organisations(
'list',
[
'droits' => 'add' === $this->request->params['action']
? ListeDroit::CREER_UTILISATEUR
: ListeDroit::MODIFIER_UTILISATEUR
]
);
$this->set(compact('mesOrganisations'));
$this->edit(null);
}
/**
......@@ -347,94 +230,52 @@ class ResponsablesController extends AppController {
* @created 20/12/2017
* @version V1.0.0
*/
public function edit($id = null) {
if (true !== $this->Droits->authorized(ListeDroit::GESTION_CORESPONSABLE)) {
throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage'));
public function edit($id) {
if ($this->request->params['action'] === 'add') {
$this->set('title', __d('responsable', 'responsable.titreAjouterResponsable'));
} else {
$responsable = $this->Droits->getAndCheckLinkedOrganisationsRecord('Responsable', $id, false);
$this->set('title', __d('responsable', 'responsable.titreModifierResponsable'));
}
$this->set('title', __d('responsable', 'responsable.titreModifierResponsable'));
if ($this->request->is('post') || $this->request->is('put')) {
if ('Cancel' === Hash::get($this->request->data, 'submit')) {
$this->redirect($this->Referers->get());
}
$success = true;
$this->Responsable->begin();
$this->request->data['Responsable']['id'] = $id;
$this->Responsable->create($this->request->data['Responsable']);
$success = $success && false !== $this->Responsable->save();
if ($success == true) {
$responsableId = $this->Responsable->id;
$success = $this->ResponsableOrganisation->deleteAll([
'ResponsableOrganisation.responsable_id' => $responsableId
]) && $success;
if ($success == true && !empty($this->request->data['ResponsableOrganisation']['organisation_id'])) {
foreach ($this->request->data['ResponsableOrganisation']['organisation_id'] as $organisationId) {
if ($success == true) {
$this->ResponsableOrganisation->create([
'responsable_id' => $responsableId,
'organisation_id' => $organisationId
]);
$success = $success && false !== $this->ResponsableOrganisation->save();
}
}
if ($this->request->params['action'] === 'add') {
$data = $this->request->data;
$data['Responsable']['createdbyorganisation'] = $this->Droits->isSu() ? null : $this->Session->read('Organisation.id');
} else {
$data = $this->request->data;
foreach (['id', 'createdbyorganisation'] as $fieldName) {
$data['Responsable'][$fieldName] = $responsable['Responsable'][$fieldName];
}
}
if ($success == true) {
$this->Responsable->commit();
$this->Session->setFlash(__d('role', 'role.flashsuccessProfilEnregistrer'), 'flashsuccess');
$this->Responsable->create($data);
if (false !== $this->Responsable->save(null, ['atomic' => true])) {
$this->Session->setFlash(__d('responsable', 'responsable.flashsuccessSaveResponsable'), 'flashsuccess');
$this->redirect($this->Referers->get());
} else {
$this->Responsable->rollback();
$this->Session->setFlash(__d('role', 'role.flasherrorErreurEnregistrementProfil'), 'flasherror');
$this->Session->setFlash(__d('responsable', 'responsable.flasherrorSaveResponsable'), 'flasherror');
}
} elseif ($this->request->params['action'] === 'add') {
$this->request->data['Organisation']['Organisation'] = $this->Droits->isSu() ? null : $this->Session->read('Organisation.id');
} else {
$this->request->data = $responsable;
}
$responsable = $this->Responsable->find('first', [
'conditions' => [
'id' => $id
]
]);
$this->request->data = $responsable;
$responsableOrganisation = $this->ResponsableOrganisation->find('all', [
'conditions' => [
'responsable_id' => $id
]
]);
$organisationResponsableSelect = Hash::extract($responsableOrganisation, '{n}.ResponsableOrganisation.organisation_id');
$optionsAffecter = [
'ResponsableOrganisation' => [
'organisation_id' => $organisationResponsableSelect
]
];
$this->request->data += $optionsAffecter;
$mesOrganisations = $this->WebcilUsers->organisations(
'list',
[
'droits' => 'add' === $this->request->params['action']
? ListeDroit::CREER_UTILISATEUR
: ListeDroit::MODIFIER_UTILISATEUR
]
);
$mesOrganisations = $this->_mesOrganisations();
$this->set(compact('mesOrganisations'));
$this->view = 'edit';
}
/**
* Permet la visualisation des informations d'un responsable
*
* @param type $id | Id du responsable
* @param int $id | Id du responsable
*
* @author Théo GUILLON <theo.guillon@libriciel.coop>
* @access public
......@@ -442,48 +283,45 @@ class ResponsablesController extends AppController {
* @version v1.0.0
*/
public function show($id) {
if (true !== $this->Droits->authorized(ListeDroit::GESTION_CORESPONSABLE)) {
throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage'));
}
$this->set('title', __d('responsable', 'responsable.titreVisualiserResponsable'));
$responsable = $this->Responsable->find('first', [
'conditions' => [
'id' => $id
]
]);
$this->request->data = $responsable;
$responsableOrganisation = $this->ResponsableOrganisation->find('all', [
$query = [
'fields' => $this->Responsable->fields(),
'contain' => [
'Organisation' => [
'fields' => [
'id'
]
]
],
'conditions' => [
'responsable_id' => $id
]
]);
$organisationResponsableSelect = Hash::extract($responsableOrganisation, '{n}.ResponsableOrganisation.organisation_id');
$optionsAffecter = [
'ResponsableOrganisation' => [
'organisation_id' => $organisationResponsableSelect
'Responsable.id' => $id
]
];
$this->request->data += $optionsAffecter;
$mesOrganisations = $this->WebcilUsers->organisations(