104 lines
2.8 KiB
PHP
104 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Urls\StoreUrlRequest;
|
|
use App\Http\Requests\Bo\Urls\UpdateUrlRequest;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Models\Brand;
|
|
use App\Models\Url;
|
|
|
|
class UrlController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
return view('bo.pages.urls.list', [
|
|
'urls' => Url::query()->paginate(10)
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('bo.pages.urls.create', [
|
|
'brands' => Brand::all()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param StoreUrlRequest $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(StoreUrlRequest $request)
|
|
{
|
|
$newUrl = new Url();
|
|
$newUrl->fill($request->validated());
|
|
$newUrl->brand()->associate($request->brand);
|
|
$newUrl->saveOrFail();
|
|
return redirect()->route('bo.urls.index')
|
|
->with(['success' => 'L\'url pour la clé ' . $newUrl->key . ' a bien été créée.']);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param Url $url
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit(Url $url)
|
|
{
|
|
return view('bo.pages.urls.edit', [
|
|
'url' => $url,
|
|
'brands' => Brand::all()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param UpdateUrlRequest $request
|
|
* @param Url $url
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(UpdateUrlRequest $request, Url $url)
|
|
{
|
|
$oldUrl = $url->value;
|
|
ToolboxHelper::saveUrlHistory($url, $oldUrl);
|
|
$url->fill($request->validated());
|
|
$url->brand()->associate($request->brand);
|
|
$url->saveOrFail();
|
|
return redirect()->route('bo.urls.index')
|
|
->with(['success' => 'L\'url pour la clé ' . $url->key . ' a bien été modifiée.']);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param Url $url
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(Url $url)
|
|
{
|
|
foreach ($url->history as $history) {
|
|
$history->deleteOrFail();
|
|
}
|
|
foreach ($url->stats as $stats) {
|
|
$stats->deleteOrFail();
|
|
}
|
|
$url->deleteOrFail();
|
|
return redirect()->route('bo.urls.index')
|
|
->with(['warning' => 'L\'url pour la clé ' . $url->key . ' a bien été supprimée.']);
|
|
}
|
|
}
|