From b22344b2ebc988c4a5176735a3521f06198f3d21 Mon Sep 17 00:00:00 2001 From: Andrey Dmitrakovich Date: Fri, 17 Jan 2025 00:01:03 +0300 Subject: [PATCH] 70. generate new slugs in admin panel --- .../Admin/Controllers/ProductController.php | 27 ++++++++----------- src/app/Http/Middleware/DeviceDetect.php | 2 ++ src/app/Listeners/Product/GenerateSlug.php | 19 +++++++++++++ src/app/Providers/EventServiceProvider.php | 2 ++ 4 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 src/app/Listeners/Product/GenerateSlug.php diff --git a/src/app/Admin/Controllers/ProductController.php b/src/app/Admin/Controllers/ProductController.php index 27ad22b1..92902e62 100644 --- a/src/app/Admin/Controllers/ProductController.php +++ b/src/app/Admin/Controllers/ProductController.php @@ -178,8 +178,6 @@ public function restore(int $productId) */ protected function form() { - abort(400, 'Проводятся технические работы. Временно не доступно.'); - $form = new Form(new Product()); $product = null; $productFromStock = $this->getStockProduct(); @@ -198,7 +196,7 @@ protected function form() }); } - $form->column(6, function ($form) use ($productFromStock) { + $form->column(6, function (Form $form) use ($productFromStock) { $form->html(function ($form) { if ($form->model()->trashed()) { return '

Товар удален

'; @@ -208,19 +206,19 @@ protected function form() $uploadImagesService = $this->uploadImagesService; $form->html(fn ($form) => $uploadImagesService->show($form->model()->getMedia()))->setWidth(12, 0); $form->html($this->uploadImagesService->getImagesInput(), 'Картинки'); - - $defaultSlug = null; - if (!empty($productFromStock->brand_id) && !empty($productFromStock->sku)) { - $defaultSlug = $this->generateSlug($productFromStock->brand_id, $productFromStock->sku); + if ($form->isCreating()) { + $form->hidden('slug', __('Slug'))->default('temp_slug_' . time()); + $form->hidden('old_slug', 'Old slug')->default('temp_old_slug_' . time()); + } else { + $form->text('slug', __('Slug'))->disable(); } - $form->text('slug', __('Slug'))->default($defaultSlug); $form->text('path', 'Путь')->disable(); $form->text('sku', 'Артикул')->required()->default($productFromStock->sku); $form->currency('buy_price', 'Цена покупки')->symbol('BYN'); $form->currency('price', 'Цена')->symbol('BYN')->required(); $form->currency('old_price', 'Старая цена')->symbol('BYN'); }); - $form->column(6, function ($form) use ($product, $productFromStock) { + $form->column(6, function (Form $form) use ($product, $productFromStock) { $form->multipleSelect('sizes', 'Размеры')->options(Size::pluck('name', 'id'))->default($productFromStock->getAvailableSizeIds())->required(); $form->multipleSelect('colors', 'Цвет для фильтра')->options(Color::orderBy('name')->pluck('name', 'id')); $form->multipleSelect('fabrics', 'Материал для фильтра')->options(Fabric::orderBy('name')->pluck('name', 'id')); @@ -270,8 +268,8 @@ protected function form() if (!$this->checkIfMediaAdded($form)) { return $this->mediaNotAddedError(); } - if (empty($form->slug)) { - $form->slug = $this->generateSlug($form->brand_id, $form->sku); + if ($form->isCreating()) { + $form->old_slug = $this->generateOldSlug($form->brand_id, $form->sku); } if (is_null($form->manufacturer_id)) { $form->manufacturer_id = 0; @@ -326,9 +324,6 @@ protected function form() } } - $form->model()->url()->delete(); - $form->model()->url()->create(['slug' => $form->slug]); - if ($form->isCreating()) { event(new ProductCreated($form->model()->refresh())); } @@ -385,9 +380,9 @@ private function productGroupGrid(Product $product) } /** - * Generate a slug based on brand name and SKU. + * Generate a old slug based on brand name and SKU. */ - private function generateSlug(int $brandId, string $sku): string + private function generateOldSlug(int $brandId, string $sku): string { return Str::slug(Brand::where('id', $brandId)->value('name') . '-' . $sku); } diff --git a/src/app/Http/Middleware/DeviceDetect.php b/src/app/Http/Middleware/DeviceDetect.php index 9b49e50c..91fd5484 100644 --- a/src/app/Http/Middleware/DeviceDetect.php +++ b/src/app/Http/Middleware/DeviceDetect.php @@ -53,6 +53,8 @@ private function getDeviceWebId(Request $request): ?string /** * Check if the current request is from a robot + * + * @todo identify robots by ip */ private function isRobot(Request $request): bool { diff --git a/src/app/Listeners/Product/GenerateSlug.php b/src/app/Listeners/Product/GenerateSlug.php new file mode 100644 index 00000000..3a0f105c --- /dev/null +++ b/src/app/Listeners/Product/GenerateSlug.php @@ -0,0 +1,19 @@ +product->update([ + 'slug' => Str::slug($event->product->shortName()), + ]); + } +} diff --git a/src/app/Providers/EventServiceProvider.php b/src/app/Providers/EventServiceProvider.php index b37cc2ae..653594f8 100644 --- a/src/app/Providers/EventServiceProvider.php +++ b/src/app/Providers/EventServiceProvider.php @@ -19,6 +19,7 @@ use App\Listeners\OneC; use App\Listeners\Order\CreateInstallment; use App\Listeners\Order\DistributeOrder; +use App\Listeners\Product; use App\Listeners\Promo\ApplyPendingPromocode; use App\Listeners\SaveDevice; use App\Listeners\SendOrderInformationNotification; @@ -98,6 +99,7 @@ class EventServiceProvider extends ServiceProvider ConvertVideo::class, ], Products\ProductCreated::class => [ + Product\GenerateSlug::class, OneC\UpdateProduct::class, ], Products\ProductUpdated::class => [