105 lines
3.4 KiB
PHP
105 lines
3.4 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;
|
|
$pdf->save();
|
|
|
|
return redirect()->route('bo.public-pdfs.index');
|
|
}
|
|
|
|
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;
|
|
$pdf->save();
|
|
|
|
return redirect()->route('bo.public-pdfs.edit', $pdf->id)->with('success', 'Enregistré !');
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$pdf = PublicPdfs::findOrFail($id);
|
|
|
|
if (file_exists(public_path() . '/' . $pdf->file_path))
|
|
unlink(public_path() . '/' . $pdf->file_path);
|
|
|
|
$pdf->delete();
|
|
|
|
return redirect()->route('bo.public-pdfs.index');
|
|
}
|
|
}
|