166 lines
5.6 KiB
PHP
166 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Commitments\StoreCommitmentRequest;
|
|
use App\Http\Requests\Bo\Commitments\UpdateCommitmentRequest;
|
|
use App\Models\Commitment;
|
|
use App\Traits\Controllers\ChangesModelOrder;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class CommitmentController extends Controller
|
|
{
|
|
use UpdateModelPublished;
|
|
use ChangesModelOrder;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Commitment::class);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $commitmentModels */
|
|
$commitmentModels = Commitment::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$commitmentModels,
|
|
$search,
|
|
null,
|
|
'title',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
Str::ucfirst(trans('validation.attributes.title'))
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($commitmentModels);
|
|
|
|
/** Custom pagination */
|
|
$commitmentModels = $this->paginate($commitmentModels);
|
|
|
|
return view('bo.pages.commitments.index', compact('commitmentModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Commitment $commitment
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Commitment $commitment): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.commitments.create', ['commitmentModel' => $commitment]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Commitments\StoreCommitmentRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreCommitmentRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$commitment = new Commitment();
|
|
|
|
if ($commitment->fill($request->validated())->saveOrFail()) {
|
|
return redirect()->route('bo.commitments.edit', $commitment)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(__('models.classes.commitment'))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(__('models.classes.commitment'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param App\Models\Commitment $commitment
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Commitment $commitment): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.commitments.edit', ['commitmentModel' => $commitment]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Commitments\UpdateCommitmentRequest $request
|
|
* @param \App\Models\Commitment $commitment
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateCommitmentRequest $request, Commitment $commitment): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $commitment) {
|
|
if ($commitment->fill($request->validated())->saveOrFail()) {
|
|
return redirect()->route('bo.commitments.edit', $commitment)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(__('models.classes.commitment'))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.commitments.edit', $commitment)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(__('models.classes.commitment'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Commitment $commitment
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Commitment $commitment): \Illuminate\Http\RedirectResponse
|
|
{
|
|
$commitment->products()->detach();
|
|
|
|
if ($commitment->explorerpage()->count() > 3) {
|
|
if ($commitment->deleteOrFail()) {
|
|
return redirect()->route('bo.commitments.index')
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(__('models.classes.commitment'))
|
|
]));
|
|
}
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(__('models.classes.commitment'))
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\Commitment $commitment
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(Commitment $commitment): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($commitment->replicate());
|
|
}
|
|
}
|