Skip to content

Commit

Permalink
176: fix some problems with document upload
Browse files Browse the repository at this point in the history
- missing entry in .htaccess
- simplify some complexity
- extracted render components
- add disclaimer about supported file formats
  • Loading branch information
Atmos4 committed Mar 22, 2024
1 parent c4e7253 commit 716e5eb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .htaccess
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.webp|\.gif|\.jpeg|\.zip|\.css|\.svg|\.js|\.ttf|\.woff2|\.webmanifest)$
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.webp|\.gif|\.jpeg|\.zip|\.css|\.svg|\.js|\.ttf|\.woff2|\.webmanifest|\.pdf|\.doc|\.docx|\.xls|\.xlsx)$
RewriteRule (.*) routes.php [QSA,L]
26 changes: 9 additions & 17 deletions app/pages/shared_documents/add_shared_document.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
$v = new Validator();
$file_upload = $v->upload("file_upload")->required()->mime(UploadField::$FILE_MIME);
$permission = $v->select("permission")->options(["USER" => 'Public', "COACH" => "Admin"])->label("Niveau de permission");
$name = $v->text("name")->label("Nom du fichier");
$name = $v->text("name")->label("Nom du fichier (optionel)");


if ($v->valid()) {
Expand All @@ -28,20 +28,12 @@
</nav>
<form method="post" enctype="multipart/form-data">
<?= $v->render_validation() ?>
<div>
<?= $file_upload->render() ?>
</div>

<div class="col-sm-6 col-12">
<?= $permission->render() ?>
</div>

<div class="col-sm-6 col-12">
<?= $name->render() ?>
</div>
<div>
<button type=" submit" class="outline">
Enregistrer
</button>
</div>
<i>Formats autorisés: images, PDF, Word, Excel. S'il manque des formats <a href="/feedback">faites une
suggestion!</a></i>
<?= $file_upload->render() ?>
<?= $permission->render() ?>
<?= $name->render() ?>
<button type=" submit" class="outline">
Enregistrer
</button>
</form>
53 changes: 53 additions & 0 deletions app/pages/shared_documents/renderDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

function renderDocument(SharedFile $shared_doc)
{
$file_mime = $shared_doc->mime;

$file_icon = match ($file_mime) {
'pdf' => 'fas fa-file-pdf',
'png', 'jpg', 'jpeg', 'gif' => 'fas fa-file-image',
'doc', 'docx' => 'fas fa-file-word',
'xls', 'xlsx' => 'fas fa-file-excel',
'ppt', 'pptx' => 'fas fa-file-powerpoint',
default => 'fas fa-file',
};
?>
<tr class="event-row">
<td>
<i class="<?= $file_icon ?> fa-lg"></i>
</td>
<td>
<a href="/telecharger?id=<?= $shared_doc->id ?>" hx-boost=false>
<?= $shared_doc->name ?>
<i class="fas fa-download"></i>
</a>
</td>
<td>
<?= $shared_doc->date->format("d/m/Y") ?>
</td>
<td>
<a href="/documents/<?= $shared_doc->id ?>/supprimer" class="destructive">
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
<?php }

function renderTable($title, $docs)
{
if (!$docs)
return; ?>
<thead>
<tr>
<th colspan="4">
<?= $title ?>
</th>
</tr>
</thead>
<tbody>
<?php foreach ($docs as $doc) {
renderDocument($doc);
} ?>
</tbody>
<?php }
110 changes: 18 additions & 92 deletions app/pages/shared_documents/shared_documents.php
Original file line number Diff line number Diff line change
@@ -1,114 +1,40 @@
<?php
restrict_access();

$can_edit = check_auth(Access::$ADD_EVENTS);
$canEdit = check_auth(Access::$ADD_EVENTS);

# public files
$shared_files_users = em()->getRepository(SharedFile::class)->findBy(['permission_level' => Permission::USER]);
$publicDocs = SharedFile::findBy(Permission::USER);

# upper authorisation files
if ($can_edit) {
$shared_files_coach_staff = em()->getRepository(SharedFile::class)->findBy(['permission_level' => Access::$ADD_EVENTS]);
}

function render_documents($shared_doc)
{
$file_mime = $shared_doc->mime;

switch ($file_mime) {
case 'pdf':
$file_icon = 'fas fa-file-pdf';
break;
case 'png':
case 'jpg':
case 'jpeg':
case 'gif':
$file_icon = 'fas fa-file-image';
break;
case 'doc':
case 'docx':
$file_icon = 'fas fa-file-word';
break;
case 'xls':
case 'xlsx':
$file_icon = 'fas fa-file-excel';
break;
case 'ppt':
case 'pptx':
$file_icon = 'fas fa-file-powerpoint';
break;
default:
$file_icon = 'fas fa-file';
break;
} ?>
<tr class="event-row clickable" onclick="window.location.href = '/telecharger?id=<?= $shared_doc->id ?>'">
<td>
<i class="<?= $file_icon ?> fa-lg"></i>
</td>
<td>
<?= $shared_doc->name ?>
</td>
<td>
<?= $shared_doc->date->format("d/m/Y") ?>
</td>
<td><a href="/documents/<?= $shared_doc->id ?>/supprimer" class="destructive">
<i class="fas fa-trash"></i>
</a></td>
</tr>
<?php }
$adminDocs = $canEdit ? SharedFile::findBy(Access::$ADD_EVENTS) : null;

include __DIR__ . "/renderDocument.php";
page("Documents partagés");
?>

<?php if ($can_edit): ?>
<?php if ($canEdit): ?>
<nav id="page-actions">
<a href="/documents/ajouter"><i class="fas fa-plus"></i> Ajouter un document</a>
</nav>
<?php endif ?>

<?php if (count($shared_files_users)): ?>
<?php if ($can_edit): ?>
<h2>Documents publics</h2>
<?php endif ?>
<table role="grid">
<thead class=header-responsive>
<tr>
<th></th>
<th>Nom du fichier</th>
<th>Date d'ajout</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
if (!$publicDocs && !$adminDocs): ?>

<?php foreach ($shared_files_users as $shared_file) {
render_documents($shared_file);
} ?>
<p class=center>Aucun document pour le moment 🫠</p>

</tbody>
</table>
<?php endif ?>
<?php return;
endif;
?>

<?php if ($can_edit && count($shared_files_coach_staff)): ?>
<h2>Documents admins</h2>
<table role="grid">
<thead class=header-responsive>
<tr>
<th></th>
<th>Nom du fichier</th>
<th></th>
</tr>
</thead>
<tbody>
<table role="grid">

<?php foreach ($shared_files_coach_staff as $shared_file) {
render_documents($shared_file);
} ?>
<?php
renderTable("Documents publics", $publicDocs);

</tbody>
</table>
<?php endif ?>
if ($adminDocs)
renderTable("Documents privés", $adminDocs);
?>

<?php if (!count($shared_files_users) && (!$can_edit || !count($shared_files_coach_staff))): ?>
<p class=center>Aucun document pour le moment 🫠</p>
<?php endif ?>
</table>
6 changes: 6 additions & 0 deletions database/models/files.db.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ static function get($file_id): SharedFile|null
return em()->find(SharedFile::class, $file_id);
}

/** @return self[] */
static function findBy($permission): array
{
return em()->getRepository(self::class)->findBy(['permission_level' => $permission]);
}

}

0 comments on commit 716e5eb

Please sign in to comment.