authorizeResource(Url::class); } /** * Display a listing of the resource. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ public function index(Request $request) { /** @var \App\Models\User $user */ $user = auth('backend')->user(); /** @var \Illuminate\Database\Eloquent\Builder $urls */ $urls = $user->role->equals(RoleEnum::conceptor) ? Url::query() : Url::query()->whereIn('brand_id', $user->brands->pluck('id')); if ($request->search) { $this->searchQuery( $urls, $request->search, null, 'key', 'value', 'title', 'brand_id' ); } $this->sortQuery($urls); $searchFields = trans('Clé, Redirection, Titre, Marque'); $urls = $this->paginate($urls); return view('bo.pages.urls.index', compact('urls', 'searchFields')); } /** * Show the form for creating a new resource. * * @param \App\Models\Url $url * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ public function create(Url $url) { /** @var \App\Models\User $user */ $user = auth('backend')->user(); $brandModels = $user->getRelatedBrands(); return view('bo.pages.urls.create', [ 'brands' => $brandModels, 'urlModel' => $url ]); } /** * Store a newly created resource in storage. * * @param StoreUrlRequest $request * @return \Illuminate\Http\RedirectResponse */ public function store(StoreUrlRequest $request) { return DB::transaction(function () use ($request) { $url = new Url(); $url->fill($request->validated()); // When duplicated, $url->logo is the path of the logo from the original model. $url->logo = is_string($url->logo) ? new UploadedFile( FileHelper::getPublicAbsolutePath($url->logo), sprintf( '%s.%s', Str::uuid(), new \SplFileInfo(asset($url->logo))->getExtension() ) ) : $url->logo; $url->saveOrFail(); // Reassign the logo in image settings to save only the path. // Do it after the first saveOrFail to get the real logo path, not tmp. $settings = json_decode($request->validated('image_settings')); $settings->image = $url->logo; $url->image_settings = $settings; if ($url->saveOrFail()) { return redirect() ->route('bo.urls.edit', $url) ->with('success', trans( ':model a été créé', ['model' => Str::singular(trans('models.classes.urls'))] )); } return redirect() ->back() ->with('error', trans( ':model n\'a pas été créé', ['model' => Str::singular(trans('models.classes.urls'))] )); }); } /** * Show the form for editing the specified resource. * * @param \App\Models\Url $url * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ public function edit(Url $url) { /** @var \App\Models\User $user */ $user = auth('backend')->user(); $brandModels = $user->getRelatedBrands(); return view('bo.pages.urls.edit', [ 'urlModel' => $url, 'brands' => $brandModels ]); } /** * Update the specified resource in storage. * * @param UpdateUrlRequest $request * @param Url $url * @return \Illuminate\Http\RedirectResponse */ public function update(UpdateUrlRequest $request, Url $url) { return DB::transaction(function () use ($request, $url) { $oldUrl = $url->value; $data = $request->validated(); // Reassign the logo in image settings to save only the path. // Do it after the first saveOrFail to get the real logo path, not tmp. $settings = json_decode($request->validated('image_settings')); if (empty($settings->image)) { $url->logo = null; } $url->fill($data); $url->saveOrFail(); if ($oldUrl !== $url->value) { $this->saveUrlHistory($url, $oldUrl); } $settings->image = $url->logo; $url->image_settings = $settings; if ($url->saveOrFail()) { return redirect() ->route('bo.urls.edit', $url) ->with('success', trans( ':model a été mis à jour', ['model' => Str::singular(trans('models.classes.urls'))] )); } return redirect() ->back() ->with('error', trans( ':model n\'a pas été mis à jour', ['model' => Str::singular(trans('models.classes.urls'))] )); }); } /** * Remove the specified resource from storage. * * @param Url $url * @return \Illuminate\Http\RedirectResponse */ public function destroy(Url $url) { foreach ($url->history as $history) { $history->deleteOrFail(); } $url->deleteOrFail(); return redirect()->route('bo.urls.index') ->with(['warning' => 'L\'url pour la clé ' . $url->key . ' a bien été supprimée.']); } /** * Duplicate the specified resource. * * @param \App\Models\Url $url * @return \Illuminate\Contracts\View\View */ public function duplicate(Url $url): \Illuminate\Contracts\View\View { $duplicatedUrl = $url->replicate(['key']); return $this->create($duplicatedUrl); } /** * Save url history when updating url. * @param Url $url * @param string $oldValue * @return \App\Models\UrlHistory */ private function saveUrlHistory(Url $url, string $oldValue) { $history = new UrlHistory(); $history->value = $oldValue; $history->start_date = $url->updated_at; $history->end_date = now(); $history->url_id = $url->id; $history->saveOrFail(); return $history; } }