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 {
'Article',
'ArticleOrganisation',
'Fichierarticle',
'Fichier',
'Organisation'
];
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])) {
throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage'));
}
$this->set('title', __d('article', 'article.titreIndex'));
$condition = [];
$conditions = [];
if ($this->request->is('post')) {
// Affectation
if (!empty($this->request->data['ArticleOrganisation']['organisation_id'])) {
......@@ -94,39 +97,10 @@ class ArticlesController extends AppController {
// Filtre
if (isset($this->request->data['Filtre']) === true) {
$condition = $this->_findFiltre($condition);
$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,
];
$conditions = $this->_findFiltre($conditions);
}
}
} else {
$query = [
'fields' => [
'Article.id',
......@@ -141,12 +115,13 @@ class ArticlesController extends AppController {
'order' => ['raisonsociale']
]
],
'conditions' => $condition,
'conditions' => $conditions,
'order' => [
'Article.name ASC'
'Article.name ASC',
'Createdbyorganisation.raisonsociale ASC',
]
];
}
$this->paginate = $query;
$articles = $this->paginate($this->Article);
......@@ -159,13 +134,18 @@ class ArticlesController extends AppController {
$this->set(compact('options'));
}
private function _findFiltre($condition) {
private function _findFiltre($conditions) {
// Filtre sur le titre de l'article
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) {
......@@ -215,14 +195,14 @@ class ArticlesController extends AppController {
$organisation_id = $this->Session->read('Organisation.id');
$condition = [];
$conditions = [];
$limit = 20;
$maxLimit = 100;
if ($this->request->is('post')) {
// Filtre
if (isset($this->request->data['Filtre']) === true) {
$condition = $this->_findFiltre($condition);
$conditions = $this->_findFiltre($conditions);
// Filtre sur le nombre de traitement à l'affichage
if (!empty($this->request->data['Filtre']['nbAffichage'])) {
......@@ -238,7 +218,7 @@ class ArticlesController extends AppController {
}
$query = [
'conditions' => $condition,
'conditions' => $conditions,
'order' => [
'Article.name ASC'
],
......@@ -270,7 +250,18 @@ class ArticlesController extends AppController {
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])) {
throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage'));
}
......@@ -281,9 +272,18 @@ class ArticlesController extends AppController {
$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 ('Cancel' === Hash::get($this->request->data, 'submit')) {
$this->redirect($this->Referers->get());
$this->Session->delete('Auth.User.uuid');
}
$data = $this->request->data;
......@@ -309,30 +309,13 @@ class ArticlesController extends AppController {
$this->Article->create($data);
$success = $success && $this->Article->save(null, ['atomic' => false]);
if ($success === true) {
$articleId = $this->Article->id;
$organisationIds = $this->request->data['ArticleOrganisation']['organisation_id'];
$success = $this->ArticleOrganisation->deleteAll([
'article_id' => $articleId
]) && $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);
}
$success = $success && false !== $this->Fichierarticle->transfereSave(
$this->Article->id,
$this->Session->read('Auth.User.uuid'),
$this->Session->read('Auth.User.id')
);
}
}
......@@ -350,12 +333,15 @@ class ArticlesController extends AppController {
}
if ($this->request->params['action'] === 'edit') {
if (empty($this->request->data)) {
$article = $this->Article->find('first', [
'conditions' => [
'id' => $id
]
]);
$this->request->data = $article;
}
$articleOrganisation = $this->ArticleOrganisation->find('all', [
'conditions' => [
......@@ -365,8 +351,8 @@ class ArticlesController extends AppController {
$organisationArticleSelect = Hash::extract($articleOrganisation, '{n}.ArticleOrganisation.organisation_id');
$optionsAffecter = [
'ArticleOrganisation' => [
'organisation_id' => $organisationArticleSelect
'Organisation' => [
'Organisation' => $organisationArticleSelect
]
];
$this->request->data += $optionsAffecter;
......@@ -419,8 +405,8 @@ class ArticlesController extends AppController {
$organisationArticleSelect = Hash::extract($articleOrganisation, '{n}.ArticleOrganisation.organisation_id');
$optionsAffecter = [
'ArticleOrganisation' => [
'organisation_id' => $organisationArticleSelect
'Organisation' => [
'Organisation' => $organisationArticleSelect
]
];
$this->request->data += $optionsAffecter;
......@@ -574,7 +560,6 @@ class ArticlesController extends AppController {
public function saveFileTmp()
{
if (!empty($this->request->params['form']['fichiers'])) {
if (empty($this->Session->read('Auth.User.uuid'))) {
$this->Session->write('Auth.User.uuid', CakeText::uuid());
}
......@@ -602,9 +587,14 @@ class ArticlesController extends AppController {
move_uploaded_file($tmpFile, $path);
$json[] = [
'status' => 'success',
'filename' => $filename,
'path' => $path
];
} else {
$json[] = [
'status' => 'error',
];
}
}
......@@ -660,11 +650,21 @@ class ArticlesController extends AppController {
if ($success == true) {
$this->Fichierarticle->commit();
$this->deleteFileDisk($this->request->data('urlFile'));
$this->_deleteFileDisk($this->request->data('urlFile'));
$this->Session->setFlash(__d('annexe', 'annexe.flashsuccessAnnexeSupprimer'), 'flashsuccess');
} else {
$this->Fichierarticle->rollback();
$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
*/
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');
$files = $this->Fichier->scan_dir($dir);
......@@ -339,6 +339,8 @@ class FichesController extends AppController
$this->redirect($this->Referers->get());
}
debug($formulaire_id);
$success = true;
$this->Fiche->begin();
......@@ -426,7 +428,7 @@ class FichesController extends AppController
if (!empty($this->Session->read('Auth.User.uuid'))) {
if ($success == true) {
$useAllExtensionFiles = $this->Formulaire->find('first', [
'condititons' => [
'conditions' => [
'id' => $formulaire_id
],
'fields' => [
......@@ -732,7 +734,7 @@ class FichesController extends AppController
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');
$files = $this->Fichier->scan_dir($dir);
......
......@@ -42,6 +42,8 @@ class Article extends AppModel {
]
];
public $hasOne = 'Fichier';
/**
* hasMany associations
*
......
......@@ -145,9 +145,9 @@ class Fichier extends AppModel {
*/
public function transfereSave($idFiche, $useAllExtensionFiles, $uuidDossier, $idUser, $typages = [], $transaction = false)
{
if (!empty($uuidDossier)) {
$files = array_diff(scandir(CHEMIN_PIECE_JOINT_TMP . $idUser . DS . $uuidDossier), array('..', '.'));
$success = true;
if (!empty($uuidDossier)) {
$dir = CHEMIN_PIECE_JOINT_TMP . $idUser . DS . $uuidDossier;
$files = $this->scan_dir($dir);
......@@ -171,7 +171,7 @@ class Fichier extends AppModel {
foreach ($files as $key => $file) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
$pathFileTMP = CHEMIN_PIECE_JOINT_TMP . $idUser . DS . $uuidDossier . DS . $file;
$pathFileTMP = $dir . DS . $file;
$mime = mime_content_type($pathFileTMP);
......@@ -206,21 +206,19 @@ class Fichier extends AppModel {
$this->commit();
}
$folder = new Folder(CHEMIN_PIECE_JOINT_TMP . $idUser . DS . $uuidDossier);
$folder = new Folder($dir);
$folder->delete();
return true;
} else {
if($transaction == true) {
if ($transaction == true) {
$this->rollback();
}
return false;
}
}
return true;
} else {
$success = false;
}
return false;
return $success;
}
/**
......
......@@ -38,17 +38,13 @@ class Fichierarticle extends AppModel {
'nom' => [
'isUniqueMultiple' => [
'rule' => ['isUniqueMultiple', ['nom', 'article_id']],
'message' => 'validation.valeurDejaUtilisee'
]
],
'article_id' => [
'isUniqueMultiple' => [
'rule' => ['isUniqueMultiple', ['nom', 'article_id']],
'message' => 'validation.valeurDejaUtilisee'
'message' => 'validation.valeurNomFichierDejaUtilisee'
]
]
];
public $hasOne = 'Fichier';
/**
* belongsTo associations
*
......@@ -61,67 +57,59 @@ class Fichierarticle extends AppModel {
public $belongsTo = [
'Article' => [
'className' => 'Article',
'foreignKey' => 'articl0_id'
'foreignKey' => 'article_id'
]
];
/**
*
* @param type $idArticle
* @param type $uuidDossier
* @param type $idUser
* @param boolean $transaction La méthode doit-elle gérer elle-même une
* transaction (par défaut: false) ?
* @param int $idArticle
* @param uuid $uuidDossier
* @param int $idUser
* @param boolean $transaction La méthode doit-elle gérer elle-même une transaction (par défaut: false) ?
* @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) {
if (!empty($uuidDossier)){
$files = array_diff(scandir(CHEMIN_PIECE_JOINT_ARTICLE_TMP . $idUser . DS . $uuidDossier), array('..', '.'));
public function transfereSave($idArticle, $uuidDossier, $idUser, $transaction = false)
{
$success = true;
if (!empty($uuidDossier)) {
$dir = CHEMIN_PIECE_JOINT_ARTICLE_TMP . $idUser . DS . $uuidDossier;
$files = $this->Fichier->scan_dir($dir);
if (isset($files) && !empty($files)) {
$success = true;
if($transaction == true) {
if ($transaction == true) {
$this->begin();
}
// On verifie si le dossier file existe. Si c'est pas le cas on le cree
if (!file_exists(CHEMIN_FICHIER)) {
mkdir(CHEMIN_FICHIER, 0777, true);
mkdir(CHEMIN_PIECE_JOINT, 0777, true);
mkdir(CHEMIN_MODELES, 0777, true);
mkdir(CHEMIN_REGISTRE, 0777, true);
} else {
if (!file_exists(CHEMIN_PIECE_JOINT_ARTICLE)) {
mkdir(CHEMIN_PIECE_JOINT_ARTICLE, 0777, true);
}
}
$success = $success && create_arborescence_files();
$accepted = Configure::read('allFileAnnexeAcceptedTypes');
foreach ($files as $key => $file) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
$pathFileTMP = $dir . DS . $file;
$accepted = [
'doc',
'docx',
'ppt',
'pptx',
'gif',
'jpeg',
'jpg',
'png',
'odp',
'ods',
'odt',
'pdf'
];
$mime = mime_content_type($pathFileTMP);
if (in_array($extension, $accepted) === true) {
if (in_array($mime, $accepted) === true) {
$url = time();
$success = $success && rename(CHEMIN_PIECE_JOINT_ARTICLE_TMP . $idUser . DS . $uuidDossier . DS . $file, CHEMIN_PIECE_JOINT_ARTICLE.$url . $key . '.' . $extension);
$newNameFile = $url . $key . '.' . $extension;
$newPathFile = CHEMIN_PIECE_JOINT_ARTICLE . $newNameFile;
$success = $success && rename($pathFileTMP, $newPathFile);
if ($success) {
$this->create([
'nom' => $file,
'url' => $url . $key . '.' . $extension,
'url' => $newNameFile,
'article_id' => $idArticle
]);
$success = $success && $this->save(null, ['atomic' => false]);
......@@ -130,25 +118,23 @@ class Fichierarticle extends AppModel {
}
if ($success == true) {
if($transaction == true) {
if ($transaction == true) {
$this->commit();
}
$folder = new Folder(CHEMIN_PIECE_JOINT_ARTICLE_TMP . $idUser . DS . $uuidDossier);
$folder = new Folder($dir);
$folder->delete();
return true;
} else {
if($transaction == true) {
if ($transaction == true) {
$this->rollback();
}
return false;
}
}
return true;
} else {
$success = false;
}
return false;
return $success;
}
/**
......
<?php
if (isset($this->validationErrors['Article']) && !empty($this->validationErrors['Article'])) {
if (isset($this->validationErrors['Article']) && !empty($this->validationErrors['Article']) ||
isset($this->validationErrors['Fichierarticle']) && !empty($this->validationErrors['Fichierarticle'])
) {
?>
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"><!----></span>
......@@ -7,9 +9,17 @@ if (isset($this->validationErrors['Article']) && !empty($this->validationErrors[
Ces erreurs se sont produites:
<ul>
<?php
foreach ($this->validationErrors as $donnees) {
foreach ($donnees as $champ) {
foreach ($champ as $error) {
if (isset($this->validationErrors['Article']) && !empty($this->validationErrors['Article'])) {
foreach ($this->validationErrors['Article'] as $errorChamps) {
foreach ($errorChamps as $error) {
echo '<li>' . $error . '</li>';
}
}
}
if (isset($this->validationErrors['Fichierarticle']) && !empty($this->validationErrors['Fichierarticle'])) {
foreach ($this->validationErrors['Fichierarticle'] as $errorChamps) {
foreach ($errorChamps as $error) {
echo '<li>' . $error . '</li>';
}
}
......@@ -56,16 +66,15 @@ echo $this->WebcilForm->create('Article', [
<div class="col-md-12">
<?php
echo $this->WebcilForm->inputs([
'ArticleOrganisation.organisation_id' => [
'id' => 'organisation_id',
'Organisation.Organisation' => [
'class' => 'form-control usersDeroulant',
'options' => $mesOrganisations,
'empty' => true,
'multiple' => true,
'data-placeholder' => __d('article', 'article.placeholderSelectOrganisation'),
'label' => [
'text' => __d('article', 'article.champSelectOrganisation'),
]
],
'data-placeholder' => __d('article', 'article.placeholderSelectOrganisation'),
'empty' => true,
'multiple' => true,
],
'Article.name' => [
'id' => 'name',
......@@ -162,7 +171,7 @@ echo $this->WebcilForm->create('Article', [
<?php
foreach ($filesSave as $val) {
?>
<tr id="rowFichier<?php echo $val['Fichierarticle']['id']; ?>">
<tr id="rowArticle<?php echo $val['Fichierarticle']['id']; ?>">
<td class="col-md-1">
<i class="fa fa-file-text-o fa-lg"><!----></i>
</td>
......@@ -218,6 +227,10 @@ echo $this->WebcilForm->create('Article', [
<?php
echo $this->WebcilForm->end();
echo $this->element('Fiches/modal/modalErrorExtentionAnnexe', [
'extentionsAccepter' => __d('formulaire', 'formulaire.infoExtentionUtilisable')
]);
?>
<script type="text/javascript">
......@@ -274,11 +287,16 @@ echo $this->WebcilForm->end();
try{
var content = JSON.parse(data);
for (var key in content) {
var tr = $('<tr id="rowArticle'+key+'"></tr>')
if (content[key].status === "success") {
var tr = $('<tr id="rowArticle' + key + '"></tr>')
.append('<td class="col-md-1"><i class="fa fa-file-text-o fa-lg"><!----></i></td>')
.append('<td class="col-md-9 tdleft">'+content[key].filename+'</td>')
.append('<td class=\"col-md-2\"><button type="button" class="btn btn-warning" onclick=\"deleteFile(\''+content[key].filename+'\',\''+key+'\')\"><i class="fa fa-times-circle"><!----></i> Annuler</button></td>')
.append('<td class="col-md-9 tdleft">' + content[key].filename + '</td>')
.append('<td class=\"col-md-2\"><button type="button" class="btn btn-warning" onclick=\"deleteFile(\'' + content[key].filename + '\',\'' + key + '\')\"><i class="fa fa-times-circle"><!----></i> Annuler</button></td>')
$('#render').find('tbody').append(tr);
} else {
alert('totot