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

Correction : accès menu "Administration de l'entité" en fonction des droits

Correction : enregistrement article dans la FAQ
Correction : enregistrement fichier dans la FAQ
Correction : enregistrement annexe traitement
parent 226889ca
...@@ -28,17 +28,20 @@ class ArticlesController extends AppController { ...@@ -28,17 +28,20 @@ class ArticlesController extends AppController {
'Article', 'Article',
'ArticleOrganisation', 'ArticleOrganisation',
'Fichierarticle', 'Fichierarticle',
'Fichier',
'Organisation' 'Organisation'
]; ];
public function index() { public function index() {
$this->Session->delete('Auth.User.uuid');
if (true !== $this->Droits->authorized($this->Droits->isSu() || [ListeDroit::CREER_ARTICLE_FAQ, ListeDroit::MODIFIER_ARTICLE_FAQ, ListeDroit::SUPPRIMER_ARTICLE_FAQ])) { if (true !== $this->Droits->authorized($this->Droits->isSu() || [ListeDroit::CREER_ARTICLE_FAQ, ListeDroit::MODIFIER_ARTICLE_FAQ, ListeDroit::SUPPRIMER_ARTICLE_FAQ])) {
throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage')); throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage'));
} }
$this->set('title', __d('article', 'article.titreIndex')); $this->set('title', __d('article', 'article.titreIndex'));
$condition = []; $conditions = [];
if ($this->request->is('post')) { if ($this->request->is('post')) {
// Affectation // Affectation
if (!empty($this->request->data['ArticleOrganisation']['organisation_id'])) { if (!empty($this->request->data['ArticleOrganisation']['organisation_id'])) {
...@@ -94,59 +97,31 @@ class ArticlesController extends AppController { ...@@ -94,59 +97,31 @@ class ArticlesController extends AppController {
// Filtre // Filtre
if (isset($this->request->data['Filtre']) === true) { if (isset($this->request->data['Filtre']) === true) {
$condition = $this->_findFiltre($condition); $conditions = $this->_findFiltre($conditions);
$query = [
'contain' => [
'Organisation' => [
'raisonsociale',
'order' => ['raisonsociale']
]
],
'conditions' => $condition,
'order' => [
'Soustraitant.raisonsociale ASC'
]
];
// Filtre sur l'entité à l'origine de l'article
if (!empty($this->request->data['Filtre']['createdbyorganisation_id'])) {
$condition = [
'Organisation.id' => $this->request->data['Filtre']['createdbyorganisation_id']
];
$query = [
'contain' => [
'Organisation' => [
'raisonsociale',
'order' => ['raisonsociale']
]
],
'conditions' => $condition,
];
}
} }
} else {
$query = [
'fields' => [
'Article.id',
'Article.name',
'Article.createdbyorganisation_id',
'Createdbyorganisation.raisonsociale'
],
'contain' => [
'Createdbyorganisation',
'Organisation' => [
'raisonsociale',
'order' => ['raisonsociale']
]
],
'conditions' => $condition,
'order' => [
'Article.name ASC'
]
];
} }
$query = [
'fields' => [
'Article.id',
'Article.name',
'Article.createdbyorganisation_id',
'Createdbyorganisation.raisonsociale'
],
'contain' => [
'Createdbyorganisation',
'Organisation' => [
'raisonsociale',
'order' => ['raisonsociale']
]
],
'conditions' => $conditions,
'order' => [
'Article.name ASC',
'Createdbyorganisation.raisonsociale ASC',
]
];
$this->paginate = $query; $this->paginate = $query;
$articles = $this->paginate($this->Article); $articles = $this->paginate($this->Article);
...@@ -159,13 +134,18 @@ class ArticlesController extends AppController { ...@@ -159,13 +134,18 @@ class ArticlesController extends AppController {
$this->set(compact('options')); $this->set(compact('options'));
} }
private function _findFiltre($condition) { private function _findFiltre($conditions) {
// Filtre sur le titre de l'article // Filtre sur le titre de l'article
if (!empty($this->request->data['Filtre']['name'])) { if (!empty($this->request->data['Filtre']['name'])) {
$condition['Article.name'] = $this->request->data['Filtre']['name']; $conditions['Article.name'] = $this->request->data['Filtre']['name'];
} }
return ($condition); // Filtre sur l'entité à l'origine de article
if (!empty($this->request->data['Filtre']['createdbyorganisation_id'])) {
$conditions['Article.createdbyorganisation_id'] = $this->request->data['Filtre']['createdbyorganisation_id'];
}
return ($conditions);
} }
private function _optionsFiltre($organisation_id = null) { private function _optionsFiltre($organisation_id = null) {
...@@ -215,14 +195,14 @@ class ArticlesController extends AppController { ...@@ -215,14 +195,14 @@ class ArticlesController extends AppController {
$organisation_id = $this->Session->read('Organisation.id'); $organisation_id = $this->Session->read('Organisation.id');
$condition = []; $conditions = [];
$limit = 20; $limit = 20;
$maxLimit = 100; $maxLimit = 100;
if ($this->request->is('post')) { if ($this->request->is('post')) {
// Filtre // Filtre
if (isset($this->request->data['Filtre']) === true) { if (isset($this->request->data['Filtre']) === true) {
$condition = $this->_findFiltre($condition); $conditions = $this->_findFiltre($conditions);
// Filtre sur le nombre de traitement à l'affichage // Filtre sur le nombre de traitement à l'affichage
if (!empty($this->request->data['Filtre']['nbAffichage'])) { if (!empty($this->request->data['Filtre']['nbAffichage'])) {
...@@ -238,7 +218,7 @@ class ArticlesController extends AppController { ...@@ -238,7 +218,7 @@ class ArticlesController extends AppController {
} }
$query = [ $query = [
'conditions' => $condition, 'conditions' => $conditions,
'order' => [ 'order' => [
'Article.name ASC' 'Article.name ASC'
], ],
...@@ -269,8 +249,19 @@ class ArticlesController extends AppController { ...@@ -269,8 +249,19 @@ class ArticlesController extends AppController {
public function add() { public function add() {
return $this->edit(); return $this->edit();
} }
public function edit($id = null) { /**
* @param int $id
* @throws Exception
*
* @access public
*
* @author Théo GUILLON <theo.guillon@libriciel.coop>
* @modified 07/08/2020
* @version V2.0.0
*/
public function edit($id = null)
{
if (true !== $this->Droits->authorized($this->Droits->isSu() || [ListeDroit::CREER_ARTICLE_FAQ, ListeDroit::MODIFIER_ARTICLE_FAQ])) { if (true !== $this->Droits->authorized($this->Droits->isSu() || [ListeDroit::CREER_ARTICLE_FAQ, ListeDroit::MODIFIER_ARTICLE_FAQ])) {
throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage')); throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage'));
} }
...@@ -281,9 +272,18 @@ class ArticlesController extends AppController { ...@@ -281,9 +272,18 @@ class ArticlesController extends AppController {
$this->set('title', __d('article', 'article.titreModificationArticle')); $this->set('title', __d('article', 'article.titreModificationArticle'));
} }
if (!empty($this->Session->read('Auth.User.uuid'))) {
$dir = CHEMIN_PIECE_JOINT_ARTICLE_TMP . $this->Session->read('Auth.User.id') . DS . $this->Session->read('Auth.User.uuid');
$files = $this->Fichier->scan_dir($dir);
$this->set('files', $files);
}
if ($this->request->is(['post', 'put'])) { if ($this->request->is(['post', 'put'])) {
if ('Cancel' === Hash::get($this->request->data, 'submit')) { if ('Cancel' === Hash::get($this->request->data, 'submit')) {
$this->redirect($this->Referers->get()); $this->redirect($this->Referers->get());
$this->Session->delete('Auth.User.uuid');
} }
$data = $this->request->data; $data = $this->request->data;
...@@ -310,29 +310,12 @@ class ArticlesController extends AppController { ...@@ -310,29 +310,12 @@ class ArticlesController extends AppController {
$success = $success && $this->Article->save(null, ['atomic' => false]); $success = $success && $this->Article->save(null, ['atomic' => false]);
if ($success === true) { if ($success === true) {
$articleId = $this->Article->id; if (!empty($this->Session->read('Auth.User.uuid'))) {
$organisationIds = $this->request->data['ArticleOrganisation']['organisation_id']; $success = $success && false !== $this->Fichierarticle->transfereSave(
$this->Article->id,
$success = $this->ArticleOrganisation->deleteAll([ $this->Session->read('Auth.User.uuid'),
'article_id' => $articleId $this->Session->read('Auth.User.id')
]) && $success; );
if (!empty($organisationIds)) {
foreach ($organisationIds as $organisationId) {
if ($success == true) {
$this->ArticleOrganisation->create([
'article_id' => $articleId,
'organisation_id' => $organisationId
]);
$success = $success && false !== $this->ArticleOrganisation->save(null, ['atomic' => false]);
}
}
}
if ($success === true) {
if (!empty($this->Session->read('Auth.User.uuid'))) {
$success = $success && false !== $this->Fichierarticle->transfereSave($articleId, $this->Session->read('Auth.User.uuid'), $this->Session->read('Auth.User.id'), true);
}
} }
} }
...@@ -350,12 +333,15 @@ class ArticlesController extends AppController { ...@@ -350,12 +333,15 @@ class ArticlesController extends AppController {
} }
if ($this->request->params['action'] === 'edit') { if ($this->request->params['action'] === 'edit') {
$article = $this->Article->find('first', [ if (empty($this->request->data)) {
'conditions' => [ $article = $this->Article->find('first', [
'id' => $id 'conditions' => [
] 'id' => $id
]); ]
$this->request->data = $article; ]);
$this->request->data = $article;
}
$articleOrganisation = $this->ArticleOrganisation->find('all', [ $articleOrganisation = $this->ArticleOrganisation->find('all', [
'conditions' => [ 'conditions' => [
...@@ -365,8 +351,8 @@ class ArticlesController extends AppController { ...@@ -365,8 +351,8 @@ class ArticlesController extends AppController {
$organisationArticleSelect = Hash::extract($articleOrganisation, '{n}.ArticleOrganisation.organisation_id'); $organisationArticleSelect = Hash::extract($articleOrganisation, '{n}.ArticleOrganisation.organisation_id');
$optionsAffecter = [ $optionsAffecter = [
'ArticleOrganisation' => [ 'Organisation' => [
'organisation_id' => $organisationArticleSelect 'Organisation' => $organisationArticleSelect
] ]
]; ];
$this->request->data += $optionsAffecter; $this->request->data += $optionsAffecter;
...@@ -419,8 +405,8 @@ class ArticlesController extends AppController { ...@@ -419,8 +405,8 @@ class ArticlesController extends AppController {
$organisationArticleSelect = Hash::extract($articleOrganisation, '{n}.ArticleOrganisation.organisation_id'); $organisationArticleSelect = Hash::extract($articleOrganisation, '{n}.ArticleOrganisation.organisation_id');
$optionsAffecter = [ $optionsAffecter = [
'ArticleOrganisation' => [ 'Organisation' => [
'organisation_id' => $organisationArticleSelect 'Organisation' => $organisationArticleSelect
] ]
]; ];
$this->request->data += $optionsAffecter; $this->request->data += $optionsAffecter;
...@@ -574,7 +560,6 @@ class ArticlesController extends AppController { ...@@ -574,7 +560,6 @@ class ArticlesController extends AppController {
public function saveFileTmp() public function saveFileTmp()
{ {
if (!empty($this->request->params['form']['fichiers'])) { if (!empty($this->request->params['form']['fichiers'])) {
if (empty($this->Session->read('Auth.User.uuid'))) { if (empty($this->Session->read('Auth.User.uuid'))) {
$this->Session->write('Auth.User.uuid', CakeText::uuid()); $this->Session->write('Auth.User.uuid', CakeText::uuid());
} }
...@@ -602,9 +587,14 @@ class ArticlesController extends AppController { ...@@ -602,9 +587,14 @@ class ArticlesController extends AppController {
move_uploaded_file($tmpFile, $path); move_uploaded_file($tmpFile, $path);
$json[] = [ $json[] = [
'status' => 'success',
'filename' => $filename, 'filename' => $filename,
'path' => $path 'path' => $path
]; ];
} else {
$json[] = [
'status' => 'error',
];
} }
} }
...@@ -660,11 +650,21 @@ class ArticlesController extends AppController { ...@@ -660,11 +650,21 @@ class ArticlesController extends AppController {
if ($success == true) { if ($success == true) {
$this->Fichierarticle->commit(); $this->Fichierarticle->commit();
$this->deleteFileDisk($this->request->data('urlFile')); $this->_deleteFileDisk($this->request->data('urlFile'));
$this->Session->setFlash(__d('annexe', 'annexe.flashsuccessAnnexeSupprimer'), 'flashsuccess'); $this->Session->setFlash(__d('annexe', 'annexe.flashsuccessAnnexeSupprimer'), 'flashsuccess');
} else { } else {
$this->Fichierarticle->rollback(); $this->Fichierarticle->rollback();
$this->Session->setFlash(__d('default', 'default.flasherrorPasDroitPage'), 'flasherror'); $this->Session->setFlash(__d('default', 'default.flasherrorPasDroitPage'), 'flasherror');
} }
} }
protected function _deleteFileDisk($urlFile)
{
$targetFileDelete = CHEMIN_PIECE_JOINT_ARTICLE . $urlFile;
if (file_exists($targetFileDelete) === true) {
$file = new File($targetFileDelete);
$file->delete();
}
}
} }
...@@ -321,7 +321,7 @@ class FichesController extends AppController ...@@ -321,7 +321,7 @@ class FichesController extends AppController
*/ */
protected function _createFiche($formulaire_id, $initialisationMode) protected function _createFiche($formulaire_id, $initialisationMode)
{ {
if (empty($this->Session->read($this->Session->read('Auth.User.uuid')))) { if (!empty($this->Session->read('Auth.User.uuid'))) {
$dir = CHEMIN_PIECE_JOINT_TMP . $this->Session->read('Auth.User.id') . DS . $this->Session->read('Auth.User.uuid'); $dir = CHEMIN_PIECE_JOINT_TMP . $this->Session->read('Auth.User.id') . DS . $this->Session->read('Auth.User.uuid');
$files = $this->Fichier->scan_dir($dir); $files = $this->Fichier->scan_dir($dir);
...@@ -339,6 +339,8 @@ class FichesController extends AppController ...@@ -339,6 +339,8 @@ class FichesController extends AppController
$this->redirect($this->Referers->get()); $this->redirect($this->Referers->get());
} }
debug($formulaire_id);
$success = true; $success = true;
$this->Fiche->begin(); $this->Fiche->begin();
...@@ -426,7 +428,7 @@ class FichesController extends AppController ...@@ -426,7 +428,7 @@ class FichesController extends AppController
if (!empty($this->Session->read('Auth.User.uuid'))) { if (!empty($this->Session->read('Auth.User.uuid'))) {
if ($success == true) { if ($success == true) {
$useAllExtensionFiles = $this->Formulaire->find('first', [ $useAllExtensionFiles = $this->Formulaire->find('first', [
'condititons' => [ 'conditions' => [
'id' => $formulaire_id 'id' => $formulaire_id
], ],
'fields' => [ 'fields' => [
...@@ -732,7 +734,7 @@ class FichesController extends AppController ...@@ -732,7 +734,7 @@ class FichesController extends AppController
throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage')); throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage'));
} }
if (empty($this->Session->read($this->Session->read('Auth.User.uuid')))) { if (!empty($this->Session->read('Auth.User.uuid'))) {
$dir = CHEMIN_PIECE_JOINT_TMP . $this->Session->read('Auth.User.id') . DS . $this->Session->read('Auth.User.uuid'); $dir = CHEMIN_PIECE_JOINT_TMP . $this->Session->read('Auth.User.id') . DS . $this->Session->read('Auth.User.uuid');
$files = $this->Fichier->scan_dir($dir); $files = $this->Fichier->scan_dir($dir);
......
...@@ -42,6 +42,8 @@ class Article extends AppModel { ...@@ -42,6 +42,8 @@ class Article extends AppModel {
] ]
]; ];
public $hasOne = 'Fichier';
/** /**
* hasMany associations * hasMany associations
* *
......
...@@ -145,9 +145,9 @@ class Fichier extends AppModel { ...@@ -145,9 +145,9 @@ class Fichier extends AppModel {
*/ */
public function transfereSave($idFiche, $useAllExtensionFiles, $uuidDossier, $idUser, $typages = [], $transaction = false) public function transfereSave($idFiche, $useAllExtensionFiles, $uuidDossier, $idUser, $typages = [], $transaction = false)
{ {
if (!empty($uuidDossier)) { $success = true;
$files = array_diff(scandir(CHEMIN_PIECE_JOINT_TMP . $idUser . DS . $uuidDossier), array('..', '.'));
if (!empty($uuidDossier)) {
$dir = CHEMIN_PIECE_JOINT_TMP . $idUser . DS . $uuidDossier; $dir = CHEMIN_PIECE_JOINT_TMP . $idUser . DS . $uuidDossier;
$files = $this->scan_dir($dir); $files = $this->scan_dir($dir);
...@@ -171,7 +171,7 @@ class Fichier extends AppModel { ...@@ -171,7 +171,7 @@ class Fichier extends AppModel {
foreach ($files as $key => $file) { foreach ($files as $key => $file) {
$extension = pathinfo($file, PATHINFO_EXTENSION); $extension = pathinfo($file, PATHINFO_EXTENSION);
$pathFileTMP = CHEMIN_PIECE_JOINT_TMP . $idUser . DS . $uuidDossier . DS . $file; $pathFileTMP = $dir . DS . $file;
$mime = mime_content_type($pathFileTMP); $mime = mime_content_type($pathFileTMP);
...@@ -206,21 +206,19 @@ class Fichier extends AppModel { ...@@ -206,21 +206,19 @@ class Fichier extends AppModel {
$this->commit(); $this->commit();
} }
$folder = new Folder(CHEMIN_PIECE_JOINT_TMP . $idUser . DS . $uuidDossier); $folder = new Folder($dir);
$folder->delete(); $folder->delete();
return true;
} else { } else {
if($transaction == true) { if ($transaction == true) {
$this->rollback(); $this->rollback();
} }
return false;
} }
} }
return true; } else {
$success = false;
} }
return false; return $success;
} }
/** /**
......
...@@ -38,17 +38,13 @@ class Fichierarticle extends AppModel { ...@@ -38,17 +38,13 @@ class Fichierarticle extends AppModel {
'nom' => [ 'nom' => [
'isUniqueMultiple' => [ 'isUniqueMultiple' => [
'rule' => ['isUniqueMultiple', ['nom', 'article_id']], 'rule' => ['isUniqueMultiple', ['nom', 'article_id']],
'message' => 'validation.valeurDejaUtilisee' 'message' => 'validation.valeurNomFichierDejaUtilisee'
]
],
'article_id' => [
'isUniqueMultiple' => [
'rule' => ['isUniqueMultiple', ['nom', 'article_id']],
'message' => 'validation.valeurDejaUtilisee'
] ]
] ]
]; ];
public $hasOne = 'Fichier';
/** /**
* belongsTo associations * belongsTo associations
* *
...@@ -61,67 +57,59 @@ class Fichierarticle extends AppModel { ...@@ -61,67 +57,59 @@ class Fichierarticle extends AppModel {
public $belongsTo = [ public $belongsTo = [
'Article' => [ 'Article' => [
'className' => 'Article', 'className' => 'Article',
'foreignKey' => 'articl0_id' 'foreignKey' => 'article_id'
] ]
]; ];
/** /**
* *
* @param type $idArticle * @param int $idArticle
* @param type $uuidDossier * @param uuid $uuidDossier
* @param type $idUser * @param int $idUser
* @param boolean $transaction La méthode doit-elle gérer elle-même une * @param boolean $transaction La méthode doit-elle gérer elle-même une transaction (par défaut: false) ?
* transaction (par défaut: false) ?
* @return boolean * @return boolean
*
* @access public
*
* @author Théo GUILLON <theo.guillon@libriciel.coop>
* @modified 07/08/2020
* @version V2.0.0
*/ */
public function transfereSave($idArticle, $uuidDossier, $idUser, $transaction = false) { public function transfereSave($idArticle, $uuidDossier, $idUser, $transaction = false)
if (!empty($uuidDossier)){ {