Merge branch 'issue-5' into 'develop'

Issue 5

Closes #5

See merge request ldc-web/projets/domaine-de-lanvaux!15
This commit is contained in:
Vincent BRIDIER 2023-06-05 13:47:18 +00:00
commit ebed45df72
6 changed files with 120 additions and 6 deletions

View file

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\UpdateChangeOrderRequest;
use App\Http\Requests\StoreGammeRequest;
use App\Http\Requests\UpdateGammeRequest;
use App\Lib\ToolboxHelper;
@ -19,13 +20,13 @@ class GammesController extends Controller
public function index(): \Illuminate\Contracts\View\View
{
$gammes = collect([]);
$dbGammes = Gamme::orderBy('title')->whereNull('parent_gamme_id')
$dbGammes = Gamme::orderBy('order')->whereNull('parent_gamme_id')
->with('childGammes')->get();
/** @var \App\Models\Gamme $gamme */
foreach ($dbGammes as $gamme) {
$gammes->push($gamme);
$gamme->childGammes->sortBy('title')
$gamme->childGammes->sortBy('order')
->map(function (Gamme $childGamme) use (&$gammes) {
$gammes->push($childGamme);
});
@ -60,6 +61,7 @@ class GammesController extends Controller
return DB::transaction(function () use ($request) {
$gamme = new Gamme();
$gamme->fill($request->validated());
$gamme->order = $this->getLastOrder();
if (is_a($request->picture, "SplFileInfo")) {
$gamme->picture = ToolboxHelper::storeImage($request->picture, $gamme->getTable());
@ -134,4 +136,49 @@ class GammesController extends Controller
}
return back()->with('error', trans(__('Delete_failed')));
}
/**
* Remove the specified resource from storage.
*
* @param UpdateChangeOrderRequest $request
* @param Gamme $gamme
* @return \Illuminate\Http\RedirectResponse
* @ignore phpcs:disable Generic.Files.LineLength.TooLong
*/
public function changeOrder(UpdateChangeOrderRequest $request, Gamme $gamme): \Illuminate\Http\RedirectResponse
{
if ($request->validated()['action'] === 'up') {
$tmp = Gamme::where('order', '<', $gamme->order)
->orderBy('order', 'DESC')->first();
$newOrder = $tmp->order;
$tmp->order = $gamme->order;
$gamme->order = $newOrder;
} elseif ($request->validated()['action'] === 'down') {
$tmp = Gamme::where('order', '>', $gamme->order)
->orderBy('order', 'ASC')->first();
$newOrder = $tmp->order;
$tmp->order = $gamme->order;
$gamme->order = $newOrder;
} else {
return redirect()->route('bo.homepage_slides.index');
}
$tmp->save();
$gamme->save();
return back();
}
/**
* Get by order the last element of the list.
*
* @return integer
*/
private function getLastOrder(): int
{
$order = Gamme::select('order')->orderBy('order', 'DESC')->first();
if ($order === null) {
return 1;
}
return $order->order + 1;
}
}

View file

@ -16,7 +16,7 @@ class GammesController extends Controller
*/
public function index(): \Illuminate\Contracts\View\View
{
$gammes = Gamme::whereNull('parent_gamme_id')->orderBy('title')->get();
$gammes = Gamme::whereNull('parent_gamme_id')->orderBy('order')->get();
$gammebanner = BasicBanner::where('slug', 'main_gammebanner')->first();
$gammehook = GammeHook::where('slug', 'gamme_hook')->first();
$sousGammes = Gamme::whereNotNull('parent_gamme_id')->orderBy('parent_gamme_id')->get();
@ -35,12 +35,12 @@ class GammesController extends Controller
$gammes = collect([]);
$dbGammes = Gamme::where('id', $gamme->id)
->with('childGammes')->get();
$parent_gamme = Gamme::whereNull('parent_gamme_id')->orderBy('title')->get();
$parent_gamme = Gamme::whereNull('parent_gamme_id')->orderBy('order')->get();
/** @var \App\Models\Gamme $dbGamme */
foreach ($dbGammes as $dbGamme) {
$gammes->push($dbGamme->id);
$dbGamme->childGammes->sortBy('title')
$dbGamme->childGammes->sortBy('order')
->map(function (Gamme $childGamme) use (&$gammes) {
$gammes->push($childGamme->id);
});

View file

@ -105,7 +105,7 @@ class Gamme extends Model
*/
public function childGammes()
{
return $this->hasMany(Gamme::class, 'parent_gamme_id');
return $this->hasMany(Gamme::class, 'parent_gamme_id')->orderBy('order');
}
/**

View file

@ -0,0 +1,39 @@
<?php
use App\Models\Gamme;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddOrderToGammes extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('gammes', function (Blueprint $table) {
$table->integer('order')->after('alt_text_picture3')->default(1);
});
$i = 1;
foreach (Gamme::all() as $gamme) {
$gamme->order = $i;
$gamme->save();
$i++;
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('gammes', function (Blueprint $table) {
$table->dropColumn('order');
});
}
}

View file

@ -20,6 +20,7 @@
<th scope="col">{{ __('Image') }}</th>
<th scope="col">{{ __('Related_category') }}</th>
<th scope="col">{{ __('Related_range') }}</th>
<th scope="col">{{ __('Order') }}</th>
<th scope="col"></th>
</tr>
</thead>
@ -51,6 +52,28 @@
<p class="fst-italic text-secondary">Pas de gamme parente associée</p>
@endif
</td>
<td>
@if ($loop->first)
<a href="{{ route('bo.gammes.change-order', ['gamme' => $gamme->id, 'action' => 'down']) }}"
class="btn-link text-decoration-none">
↓
</a>
@elseif($loop->last)
<a href="{{ route('bo.gammes.change-order', ['gamme' => $gamme->id, 'action' => 'up']) }}"
class="btn-link text-decoration-none">
↑
</a>
@else
<a href="{{ route('bo.gammes.change-order', ['gamme' => $gamme->id, 'action' => 'up']) }}"
class="btn-link text-decoration-none">
↑
</a>
<a href="{{ route('bo.gammes.change-order', ['gamme' => $gamme->id, 'action' => 'down']) }}"
class="btn-link text-decoration-none">
↓
</a>
@endif
</td>
<td>
<a href="{{ route('bo.gammes.edit', ['gamme' => $gamme->id]) }}"
class="btn btn-sm btn-primary">{{ __('Edit') }}</a>

View file

@ -249,6 +249,11 @@ Route::prefix('bo')
->withoutMiddleware('htmlsanitize')
->middleware('htmlsanitize:text');
Route::get(
'/gammes/change-order/{gamme}/{action}',
[GammesController::class, 'changeOrder']
)->name('gammes.change-order');
Route::prefix('gammebanners')
->name('gammebanners.')
->group(function () {