Skip to content

Commit

Permalink
70. generate new slugs in admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrakovich committed Jan 16, 2025
1 parent 31b65fa commit b22344b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/app/Admin/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ public function restore(int $productId)
*/
protected function form()
{
abort(400, 'Проводятся технические работы. Временно не доступно.');

$form = new Form(new Product());
$product = null;
$productFromStock = $this->getStockProduct();
Expand All @@ -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 '<h4 class="text-red">Товар удален</h4>';
Expand All @@ -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'));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/app/Http/Middleware/DeviceDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
19 changes: 19 additions & 0 deletions src/app/Listeners/Product/GenerateSlug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Listeners\Product;

use App\Events\Products\ProductCreated;
use Illuminate\Support\Str;

class GenerateSlug
{
/**
* Handle the event.
*/
public function handle(ProductCreated $event): void
{
$event->product->update([
'slug' => Str::slug($event->product->shortName()),
]);
}
}
2 changes: 2 additions & 0 deletions src/app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,6 +99,7 @@ class EventServiceProvider extends ServiceProvider
ConvertVideo::class,
],
Products\ProductCreated::class => [
Product\GenerateSlug::class,
OneC\UpdateProduct::class,
],
Products\ProductUpdated::class => [
Expand Down

0 comments on commit b22344b

Please sign in to comment.