diff --git a/app/Http/Controllers/Fo/ProductController.php b/app/Http/Controllers/Fo/ProductController.php index ea98022d..23093fdc 100644 --- a/app/Http/Controllers/Fo/ProductController.php +++ b/app/Http/Controllers/Fo/ProductController.php @@ -122,37 +122,43 @@ class ProductController extends Controller */ public function getSiblingModels(Product $productModel): array { + $filter = Cache::get('productsFilter'); + $cleanedFilter = $this->cleanSubSubFilter($filter); + /** @var string $slug */ - $slug = (Cache::get('productsFilter')) - ? (((is_null($productModel->{Cache::get('productsFilter')})) - ? $productModel->{"sub" . Cache::get('productsFilter')}->{Cache::get('productsFilter')} - : $productModel->{Cache::get('productsFilter')})->slug) + $slug = $filter + ? ((is_null($productModel->{$filter}) + ? $productModel->{"sub" . $this->cleanSubSubFilter($filter)}->{$this->cleanSubSubFilter($filter)} ?? null + : $productModel->{$filter})->slug ?? "") : ""; + + // FIXME: the previous element are added on 30/10/24 to avoid error when productFilters was set on "submeat" for example and user access to product which not have submeat relation -> "Attempt to read property 'sub***' on null" + // this fix will just avoid the error, but in this case, product.show have no previous or next product. + /** @var \Illuminate\Support\Collection $productModelsList */ $productModelsList = Product::query() ->with('range', 'subrange', 'meat', 'submeat') - ->when(in_array(Cache::get('productsFilter'), ['range', 'meat']), function (Builder $q) use ($slug) { + ->when(in_array($filter, ['range', 'meat']), function (Builder $q) use ($slug, $filter) { $q->where('published', true) - ->whereHas(Cache::get('productsFilter'), function (Builder $q) use ($slug) { + ->whereHas($filter, function (Builder $q) use ($slug, $filter) { $q->where('published', true)->where('slug', $slug); }) - ->orWhereHas('sub' . Cache::get('productsFilter'), function (Builder $q) use ($slug) { + ->orWhereHas('sub' . $filter, function (Builder $q) use ($slug, $filter) { $q->where('published', true) - ->whereHas(Cache::get('productsFilter'), function (Builder $q) use ($slug) { + ->whereHas($filter, function (Builder $q) use ($slug) { $q->where('published', true)->where('slug', $slug); }); }); }) - ->when(in_array(Cache::get('productsFilter'), ['subrange', 'submeat']), function (Builder $q) use ($slug) { + ->when(in_array($filter, ['subrange', 'submeat']), function (Builder $q) use ($slug, $filter) { $q->where('published', true) - ->whereHas(Cache::get('productsFilter'), function (Builder $q) use ($slug) { + ->whereHas($filter, function (Builder $q) use ($slug) { $q->where('published', true)->where('slug', $slug); }); }) ->where('published', true) ->orderBy('slug') ->get(); - $collectionIndex = $productModelsList->search(function ($productModelItem, $key) use ($productModel) { if ($productModelItem->getKey() === $productModel->getKey()) { return $key; @@ -541,4 +547,14 @@ class ProductController extends Controller { return response()->json(Product::getProductsViewsByRange($request->LEGENDSLUG)); } + + + /** + * Delete the "sub" start filter to avoid "subsubrange" when user has some filters and hard visit a total different product filter + * @var string $filter + * + */ + private function cleanSubSubFilter($filter) { + return str_starts_with($filter, "sub") ? preg_replace('/^sub/', '', $filter) : $filter; + } } diff --git a/app/Http/Requests/Club/Customers/StoreCustomerRequest.php b/app/Http/Requests/Club/Customers/StoreCustomerRequest.php index dde3c49c..2cfad66d 100644 --- a/app/Http/Requests/Club/Customers/StoreCustomerRequest.php +++ b/app/Http/Requests/Club/Customers/StoreCustomerRequest.php @@ -54,11 +54,12 @@ class StoreCustomerRequest extends FormRequest */ public function rules(): array { + $customerId = auth('club')->user(); $countryList = implode(',', Country::getValidIsoAplha2Codes()); return [ 'lastname' => 'required|string|max:255', 'firstname' => 'required|string|max:255', - 'username' => 'required|string|max:255', + 'username' => ['required','string', 'max:255', Rule::unique('customers', 'username')->ignore($customerId)], 'phone' => "nullable|max:255|between:1,100|phone:{$countryList}", 'address' => 'required|string|min:1|max:255', 'address2' => 'nullable|string|min:1|max:255', @@ -117,4 +118,16 @@ class StoreCustomerRequest extends FormRequest 'opt_in' => trans('validation.custom.newsletter'), ]; } + + /** + * Get custom messages for validator errors. + * + * @return array + */ + public function messages(): array + { + return [ + 'username.unique' => trans('Le pseudo est déjà pris'), + ]; + } }