Skip to content

Commit

Permalink
feat(admin): added comment export
Browse files Browse the repository at this point in the history
  • Loading branch information
julsteele committed Jan 30, 2024
1 parent 6d6570f commit 21f6c13
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
44 changes: 44 additions & 0 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use App\Models\Comment;

class CommentController extends Controller
{
/**
* Export a CSV containing all comments.
*/
public function exportCommentsAdmin()
{
/** @var User $user */
$user = Auth::user();
abort_if(!$user->isAdmin(), 403, 'Insufficient permissions');

$headers = [
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-type' => 'text/csv',
'Content-Disposition' => 'attachment; filename=comments.csv',
'Expires' => '0',
'Pragma' => 'public'
];

$comments = Comment::getAllCommentsForExport();

if (!empty($comments)) {
# add table headers
array_unshift($comments, array_keys($comments[0]));
}

$callback = function () use ($comments) {
$handle = fopen('php://output', 'w');
foreach ($comments as $row) {
fputcsv($handle, $row);
}
fclose($handle);
};

return response()->stream($callback, 200, $headers)->sendContent();
}
}
18 changes: 18 additions & 0 deletions app/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,22 @@ public function author()
{
return $this->belongsTo(User::class, 'user_id');
}

public static function getAllCommentsForExport()
{
$applications = self::leftJoin('applications', 'application_id', '=', 'applications.id')
->leftJoin('users as u1', 'applications.user_id', '=', 'u1.id')
->leftJoin('users as u2', 'comments.user_id', '=', 'u2.id')
->leftJoin('table_types', 'table_type_assigned', '=', 'table_types.id')
->select(
'u1.name AS User',
'type as Type',
'text AS Comment Text',
'table_types.name AS Assigned Table',
'table_number as Table number',
'u2.name AS Author'
)
->get();
return json_decode(json_encode($applications), true);
}
}
4 changes: 4 additions & 0 deletions resources/views/filament/pages/export.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<a target="_blank" href="export/csv" class="text-secondary small">Export CSV</a>
</div>

<div class="d-grid gap-2 d-sm-flex justify-content-sm-center">
<a target="_blank" href="export/comments" class="text-secondary small">Export comments</a>
</div>

<div class="d-grid gap-2 d-sm-flex justify-content-sm-center">
<a target="_blank" href="export/appdata" class="text-secondary small">Export data for EF App</a>
</div>
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Http\Controllers\Applications\ApplicationController;
use App\Http\Controllers\CommentController;
use App\Models\Application;
use App\Models\Comment;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -62,6 +63,7 @@

Route::get('admin/export/appdata', [ApplicationController::class, 'exportAppDataAdmin']);
Route::get('admin/export/csv', [ApplicationController::class, 'exportCsvAdmin']);
Route::get('admin/export/comments', [CommentController::class, 'exportCommentsAdmin']);
});

Route::middleware(['auth:web', \App\Http\Middleware\AccessTokenValidationMiddleware::class])->group(function () {
Expand Down

0 comments on commit 21f6c13

Please sign in to comment.