fix: 🚨 fix linters errors
This commit is contained in:
parent
4bda1ee99b
commit
247d57ef6b
12 changed files with 103 additions and 67 deletions
|
|
@ -94,7 +94,7 @@ class UserCreate extends Command
|
|||
));
|
||||
continue;
|
||||
} //end try
|
||||
}; //end while
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -119,7 +119,7 @@ class UserCreate extends Command
|
|||
));
|
||||
continue;
|
||||
} //end try
|
||||
}; //end while
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -144,6 +144,6 @@ class UserCreate extends Command
|
|||
));
|
||||
continue;
|
||||
} //end try
|
||||
}; //end while
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ class HomeController extends Controller
|
|||
$threeproducts = Product::where('published', true)
|
||||
->where('is_vip', true)
|
||||
->take(3)
|
||||
->orderBy('order', 'DESC')
|
||||
->orderBy('priority_order', 'DESC')
|
||||
->with('subrange.range')
|
||||
->get();
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Http\Requests\Bo;
|
||||
|
||||
use App\Interfaces\OrderableInterface;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
|
|
@ -53,16 +54,11 @@ class UpdateOrderRequest extends FormRequest
|
|||
'priority_order' => [
|
||||
'required',
|
||||
'integer',
|
||||
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
||||
function ($attribute, $value, $fail) {
|
||||
if ($this->orderable && intval($value) < 1) {
|
||||
$fail(trans('validation.custom.min_priority'));
|
||||
}
|
||||
},
|
||||
'min:1',
|
||||
sprintf('max:%s', $this->orderable->max('priority_order')),
|
||||
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
||||
function ($attribute, $value, $fail) {
|
||||
if ($this->orderable && intval($value) === $this->orderable->priority_order) {
|
||||
function ($value, $fail) {
|
||||
// phpcs:ignore Generic.Files.LineLength.TooLong
|
||||
if ($this->orderable instanceof OrderableInterface && intval($value) === $this->orderable->getPriorityOrder()) {
|
||||
$fail(trans('validation.custom.same_priority'));
|
||||
}
|
||||
},
|
||||
|
|
|
|||
21
app/Interfaces/OrderableInterface.php
Normal file
21
app/Interfaces/OrderableInterface.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace App\Interfaces;
|
||||
|
||||
interface OrderableInterface
|
||||
{
|
||||
/**
|
||||
* Returns the priority order value.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getPriorityOrder(): int;
|
||||
|
||||
/**
|
||||
* Returns the grouping field used for ordering.
|
||||
* Returns null if the order is global.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getOrderGroupField(): ?string;
|
||||
}
|
||||
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\Advices\AdviceTypeEnum;
|
||||
use App\Interfaces\OrderableInterface;
|
||||
use App\Enums\Advices\AdviceDifficultyEnum;
|
||||
use App\Enums\Advices\AdviceTypeEnum;
|
||||
use App\Lib\Helpers\FileStorageHelper;
|
||||
use App\Lib\Helpers\ToolboxHelper;
|
||||
use App\Traits\Models\Orderable;
|
||||
|
|
@ -12,14 +13,14 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
|
||||
use Uwil\Seo\Laravel\Traits\SeoTrait;
|
||||
use Uwil\Seo\Laravel\Objects\SeoItem;
|
||||
use Spatie\Sitemap\Contracts\Sitemapable;
|
||||
use Spatie\Sitemap\Tags\Url;
|
||||
use Uwil\SitemapGenerator\Lib\SitemapHelper;
|
||||
use Uwil\Laravel\Redirections\Interfaces\RedirectableInterface;
|
||||
use Uwil\Laravel\Redirections\Traits\IsRedirectable;
|
||||
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
|
||||
use Uwil\Seo\Laravel\Objects\SeoItem;
|
||||
use Uwil\Seo\Laravel\Traits\SeoTrait;
|
||||
use Uwil\SitemapGenerator\Lib\SitemapHelper;
|
||||
|
||||
/**
|
||||
* @property integer $id Id.
|
||||
|
|
@ -62,7 +63,7 @@ use Uwil\Laravel\Redirections\Traits\IsRedirectable;
|
|||
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Tag> $tags
|
||||
* Tags Morph-To-Many relationship.
|
||||
*/
|
||||
class Advice extends Authenticatable implements SeoInterface, Sitemapable, RedirectableInterface
|
||||
class Advice extends Authenticatable implements SeoInterface, Sitemapable, RedirectableInterface, OrderableInterface
|
||||
{
|
||||
use HasFactory;
|
||||
use SeoTrait;
|
||||
|
|
@ -123,7 +124,7 @@ class Advice extends Authenticatable implements SeoInterface, Sitemapable, Redir
|
|||
{
|
||||
static::creating(function (self $model) {
|
||||
self::manageFields($model);
|
||||
self::bootOrderable($model);
|
||||
self::bootOrderable();
|
||||
});
|
||||
static::updating(function (self $model) {
|
||||
self::manageFields($model);
|
||||
|
|
|
|||
|
|
@ -2,23 +2,24 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\Sitemap\Tags\Url;
|
||||
use App\Enums\NutriscoreEnum;
|
||||
use App\Lib\Helpers\ToolboxHelper;
|
||||
use App\Interfaces\OrderableInterface;
|
||||
use App\Enums\Advices\AdviceTypeEnum;
|
||||
use App\Enums\NutriscoreEnum;
|
||||
use App\Enums\Products\ProductSpeciesEnum;
|
||||
use App\Lib\Helpers\FileStorageHelper;
|
||||
use App\Lib\Helpers\ToolboxHelper;
|
||||
use App\Traits\Models\Orderable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\Sitemap\Contracts\Sitemapable;
|
||||
use Spatie\Sitemap\Tags\Url;
|
||||
use Uwil\Laravel\Redirections\Interfaces\RedirectableInterface;
|
||||
use Uwil\Laravel\Redirections\Traits\IsRedirectable;
|
||||
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
|
||||
use Uwil\Seo\Laravel\Objects\SeoItem;
|
||||
use Uwil\Seo\Laravel\Traits\SeoTrait;
|
||||
use App\Lib\Helpers\FileStorageHelper;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Spatie\Sitemap\Contracts\Sitemapable;
|
||||
use App\Enums\Products\ProductSpeciesEnum;
|
||||
use App\Traits\Models\Orderable;
|
||||
use Uwil\SitemapGenerator\Lib\SitemapHelper;
|
||||
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
|
||||
use Uwil\Laravel\Redirections\Traits\IsRedirectable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Uwil\Laravel\Redirections\Interfaces\RedirectableInterface;
|
||||
|
||||
/**
|
||||
* @property integer $id Id.
|
||||
|
|
@ -53,7 +54,7 @@ use Uwil\Laravel\Redirections\Interfaces\RedirectableInterface;
|
|||
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\News> $news
|
||||
* News Belongs-To-Many relation.
|
||||
*/
|
||||
class Product extends Model implements SeoInterface, Sitemapable, RedirectableInterface
|
||||
class Product extends Model implements SeoInterface, Sitemapable, RedirectableInterface, OrderableInterface
|
||||
{
|
||||
use HasFactory;
|
||||
use SeoTrait;
|
||||
|
|
@ -115,9 +116,8 @@ class Product extends Model implements SeoInterface, Sitemapable, RedirectableIn
|
|||
{
|
||||
// * EVENT CLOSURES.
|
||||
static::creating(function (self $model) {
|
||||
self::setOrder($model);
|
||||
self::manageFields($model);
|
||||
self::bootOrderable($model);
|
||||
self::bootOrderable();
|
||||
});
|
||||
static::updating(function (self $model) {
|
||||
self::manageFields($model);
|
||||
|
|
|
|||
|
|
@ -3,9 +3,10 @@
|
|||
namespace App\Traits\Controllers;
|
||||
|
||||
use App\Http\Requests\Bo\UpdateOrderRequest;
|
||||
use App\Interfaces\OrderableInterface;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
trait ManagesOrder
|
||||
{
|
||||
|
|
@ -17,13 +18,13 @@ trait ManagesOrder
|
|||
* to the target model. The whole operation runs inside a
|
||||
* database transaction to ensure data consistency.
|
||||
*
|
||||
* @param UpdateOrderRequest $request The validated request containing the 'priority' field.
|
||||
* @param Model $model The model to reorder (must use the Orderable trait).
|
||||
* @param UpdateOrderRequest $request The validated request containing the 'priority' field.
|
||||
* @param Model&OrderableInterface $model The model to reorder (must use the Orderable trait).
|
||||
* @return RedirectResponse
|
||||
*
|
||||
* @throws \Throwable Returns if save fail.
|
||||
*/
|
||||
public function changeOrderPriority(UpdateOrderRequest $request, Model $model): RedirectResponse
|
||||
public function changeOrderPriority(UpdateOrderRequest $request, Model&OrderableInterface $model): RedirectResponse
|
||||
{
|
||||
$newPriority = (int) $request->validated('priority_order');
|
||||
|
||||
|
|
@ -32,8 +33,8 @@ trait ManagesOrder
|
|||
$query = $model::query()
|
||||
->whereKeyNot($model)
|
||||
->whereBetween('priority_order', [
|
||||
min($model->priority_order, $newPriority),
|
||||
max($model->priority_order, $newPriority),
|
||||
min($model->getPriorityOrder(), $newPriority),
|
||||
max($model->getPriorityOrder(), $newPriority),
|
||||
]);
|
||||
|
||||
$groupField = $model->getOrderGroupField();
|
||||
|
|
@ -41,11 +42,11 @@ trait ManagesOrder
|
|||
$query->where($groupField, $model->{$groupField});
|
||||
}
|
||||
|
||||
$model->priority_order < $newPriority
|
||||
$model->getPriorityOrder() < $newPriority
|
||||
? $query->decrement('priority_order', 1)
|
||||
: $query->increment('priority_order', 1);
|
||||
|
||||
$model->priority_order = $newPriority;
|
||||
$model->setAttribute('priority_order', $newPriority);
|
||||
$model->saveOrFail();
|
||||
|
||||
return redirect()->back()->with('success', trans('crud.messages.order_changed'));
|
||||
|
|
|
|||
|
|
@ -2,10 +2,15 @@
|
|||
|
||||
namespace App\Traits\Models;
|
||||
|
||||
use App\Interfaces\OrderableInterface;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
trait Orderable
|
||||
{
|
||||
/**
|
||||
* @property int $priority_order
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns the grouping field used for ordering.
|
||||
* Returns null if the order is global
|
||||
|
|
@ -17,23 +22,34 @@ trait Orderable
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the priority order value of the model.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getPriorityOrder(): int
|
||||
{
|
||||
return $this->priority_order ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* shifts down by 1 all elements positioned after the delete.
|
||||
*
|
||||
* @param Model $model
|
||||
* @param Model&OrderableInterface $model
|
||||
* @return void
|
||||
*/
|
||||
public static function setOrder(Model $model): void
|
||||
public static function setOrder(OrderableInterface $model): void
|
||||
{
|
||||
$groupField = $model->getOrderGroupField();
|
||||
$query = $model::query()->where('priority_order', '>', $model->priority_order);
|
||||
$query = $model::query()->where('priority_order', '>', $model->getPriorityOrder());
|
||||
|
||||
if ($groupField) {
|
||||
$query->where($groupField, $model->{$groupField});
|
||||
}
|
||||
|
||||
$query->each(function (Model $item): void {
|
||||
$item->priority_order -= 1;
|
||||
/** @var Model&OrderableInterface $item */
|
||||
$item->setAttribute('priority_order', $item->getPriorityOrder() - 1);
|
||||
$item->saveOrFail();
|
||||
});
|
||||
}
|
||||
|
|
@ -46,7 +62,8 @@ trait Orderable
|
|||
public static function bootOrderable(): void
|
||||
{
|
||||
static::creating(function (Model $model): void {
|
||||
if (!$model->priority_order) {
|
||||
/** @var Model&OrderableInterface $model */
|
||||
if (!$model->getPriorityOrder()) {
|
||||
$groupField = $model->getOrderGroupField();
|
||||
$query = static::query();
|
||||
|
||||
|
|
@ -54,7 +71,7 @@ trait Orderable
|
|||
$query->where($groupField, $model->{$groupField});
|
||||
}
|
||||
|
||||
$model->priority_order = ($query->max('priority_order') ?? 0) + 1;
|
||||
$model->setAttribute('priority_order', ($query->max('priority_order') ?? 0) + 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -141,10 +141,10 @@ return [
|
|||
'seo_title' => 'SEO title',
|
||||
'seo_description' => 'SEO description',
|
||||
'type' => 'type',
|
||||
'new_priority' => 'New priority',
|
||||
'new_priority' => 'New priority',
|
||||
'priority' => 'priority',
|
||||
'same_priority' => 'the priority value must be different from the current value.',
|
||||
'min_priority' => 'the priority value must be greater than 0.'
|
||||
'same_priority' => 'the priority value must be different from the current value.',
|
||||
'min_priority' => 'the priority value must be greater than 0.'
|
||||
|
||||
],
|
||||
];
|
||||
|
|
|
|||
|
|
@ -152,10 +152,10 @@ return [
|
|||
'seo_description' => 'description SEO',
|
||||
'type' => 'type',
|
||||
'url' => 'Le champ :field doit être une URL valide.',
|
||||
'new_priority' => 'Nouvelle priorité',
|
||||
'new_priority' => 'Nouvelle priorité',
|
||||
'priority' => 'priorité',
|
||||
'same_priority' => 'La valeur de priorité doit être différente de la valeur actuelle.',
|
||||
'min_priority' => 'La valeur de priorité doit être supérieure à 0.'
|
||||
'same_priority' => 'La valeur de priorité doit être différente de la valeur actuelle.',
|
||||
'min_priority' => 'La valeur de priorité doit être supérieure à 0.'
|
||||
],
|
||||
'attributes' => [
|
||||
'advantages' => 'Avantages',
|
||||
|
|
|
|||
|
|
@ -20,5 +20,5 @@ import "./base/common";
|
|||
import "./base/matomo";
|
||||
|
||||
// TS script
|
||||
import "./pages/contact"
|
||||
import "./pages/contact";
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const plusButton = document.querySelector<HTMLAnchorElement>('.btn-plus-wrapper');
|
||||
const form = document.getElementById('contact-form') as HTMLFormElement | null;
|
||||
const csrfToken = document.querySelector<HTMLMetaElement>('meta[name="csrf-token"]')?.content;
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const plusButton = document.querySelector<HTMLAnchorElement>(".btn-plus-wrapper");
|
||||
const form = document.getElementById("contact-form") as HTMLFormElement | null;
|
||||
const csrfToken = document.querySelector<HTMLMetaElement>("meta[name=\"csrf-token\"]")?.content;
|
||||
const saveDraftUrl = form?.dataset.saveDraftUrl;
|
||||
|
||||
if (!plusButton || !form || !csrfToken || !saveDraftUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
plusButton.addEventListener('click', (e) => {
|
||||
plusButton.addEventListener("click", (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
const href = plusButton.href;
|
||||
|
|
@ -16,15 +16,15 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
const data = Object.fromEntries(formData.entries());
|
||||
|
||||
fetch(saveDraftUrl, {
|
||||
method: 'POST',
|
||||
method: "POST",
|
||||
headers: {
|
||||
'X-CSRF-TOKEN': csrfToken,
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
"X-CSRF-TOKEN": csrfToken,
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
}).finally(() => {
|
||||
window.location.href = href;
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue