ldc-jeusselin/app/Http/Controllers/Bo/PublicPdfsController.php
Jérémy Munsch de4dcfb283
Closes #2 and #4
- Add status checker + admin secure packages + upgrade enum + add sentry + upgrade to sitemap generator + bump to CRMV2
- Set customer role enum and user role enum
- Update customer migration to set roles
- phpcbf
- faker local to fr
- update lang
- composer update
- bump laravel ui
- Redo all user crud
- npm bump
- Create admin command
- rearrange views
- redo all auth system
- redo customer pro bo
2023-08-21 15:43:33 +02:00

126 lines
4.1 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Models\PdfCategories;
use App\Models\PublicPdfs;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
class PublicPdfsController extends Controller
{
public function index(Request $request)
{
$publicpdfs = PublicPdfs::with('category')->get();
return view('bo.public-pdfs.index', compact('publicpdfs'));
}
public function create()
{
$pc = PdfCategories::all();
return view('bo.public-pdfs.create', compact('pc'));
}
public function store(Request $request)
{
$request->validate([
'pdffile' => ['required','mimes:application/pdf,pdf','max:2048'],
]);
$is_published = isset($request->is_published) ? true : false;
$pdffile = $request->pdffile;
$categoryId = $request->category;
$category = PdfCategories::findOrFail($categoryId);
$fileName = Str::slug(pathinfo($pdffile->getClientOriginalName(), PATHINFO_FILENAME)) . '.' . pathinfo($pdffile->getClientOriginalName(), PATHINFO_EXTENSION);
$pdffile->move(public_path('uploads/pdf/' . Str::slug($category->name) . '/' . date('Y') . '/' . date('m')), $fileName);
$pdf = new PublicPdfs();
$pdf->file_name = $pdffile->getClientOriginalName();
$pdf->file_path = 'uploads/pdf/' . Str::slug($category->name) . '/' . date('Y') . '/' . date('m') . '/' . $fileName;
$pdf->is_published = $is_published;
$pdf->category_id = $categoryId;
if ($pdf->saveOrFail()) {
return redirect()
->route('bo.public-pdfs.index')
->with('success', trans(
':model a été crée',
['model' => 'PDF']
));
}
return back();
}
public function edit($id)
{
$pdf = PublicPdfs::findOrFail($id);
$pc = PdfCategories::all();
return view('bo.public-pdfs.edit')
->with('pdf', $pdf)
->with('pc', $pc);
}
public function update(Request $request, $id)
{
$request->validate([
'pdffile' => ['mimes:application/pdf,pdf','max:2048'],
]);
$is_published = isset($request->is_published) ? true : false;
$pdffile = $request->pdffile;
$categoryId = $request->category;
$category = PdfCategories::findOrFail($categoryId);
$pdf = PublicPdfs::findOrFail($id);
if ($pdffile !== null) {
if (file_exists(public_path() . '/' . $pdf->file_path)) {
unlink(public_path() . '/' . $pdf->file_path);
}
$fileName = Str::slug(pathinfo($pdffile->getClientOriginalName(), PATHINFO_FILENAME)) . '.' . pathinfo($pdffile->getClientOriginalName(), PATHINFO_EXTENSION);
$pdffile->move(public_path('uploads/pdf/' . Str::slug($category->name) . '/' . date('Y') . '/' . date('m')), $fileName);
$pdf->file_name = $pdffile->getClientOriginalName();
$pdf->file_path = 'uploads/pdf/' . Str::slug($category->name) . '/' . date('Y') . '/' . date('m') . '/' . $fileName;
}
$pdf->is_published = $is_published;
$pdf->category_id = $categoryId;
if ($pdf->saveOrFail()) {
return redirect()
->route('bo.public-pdfs.index')
->with('success', trans(
':model a été mis à jour',
['model' => 'PDF']
));
}
return back();
}
public function destroy($id)
{
$pdf = PublicPdfs::findOrFail($id);
if (file_exists(public_path() . '/' . $pdf->file_path)) {
unlink(public_path() . '/' . $pdf->file_path);
}
if ($pdf->deleteOrFail()) {
return redirect()
->route('bo.public-pdfs.index')
->with('warning', trans(
':model a bien été supprimé',
['model' => 'PDF']
));
}
return back();
}
}