133 lines
4.3 KiB
PHP
133 lines
4.3 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(
|
|
storage_path(sprintf('app/public/modelfiles/public_pdfs/%s/%s/%s', Str::slug($category->name), date('Y'), date('m'))),
|
|
$fileName
|
|
);
|
|
|
|
$pdf = new PublicPdfs();
|
|
$pdf->file_name = $pdffile->getClientOriginalName();
|
|
$pdf->file_path = sprintf('storage/modelfiles/public_pdfs/%s/%s/%s/%s', 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(storage_path(Str::replaceFirst('storage', 'app/public', $pdf->file_path)))) {
|
|
unlink(storage_path(Str::replaceFirst('storage', 'app/public', $pdf->file_path)));
|
|
}
|
|
$fileName = Str::slug(pathinfo($pdffile->getClientOriginalName(), PATHINFO_FILENAME)) . '.' . pathinfo($pdffile->getClientOriginalName(), PATHINFO_EXTENSION);
|
|
$pdffile->move(
|
|
storage_path(sprintf('app/public/modelfiles/public_pdfs/%s/%s/%s', Str::slug($category->name), date('Y'), date('m'))),
|
|
$fileName
|
|
);
|
|
|
|
$pdf->file_name = $pdffile->getClientOriginalName();
|
|
$pdf->file_path = sprintf('storage/modelfiles/public_pdfs/%s/%s/%s/%s', 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(storage_path(Str::replaceFirst('storage', 'app/public', $pdf->file_path)))) {
|
|
unlink(storage_path(Str::replaceFirst('storage', 'app/public', $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();
|
|
}
|
|
}
|