120 lines
4.5 KiB
PHP
120 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Shop;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Product;
|
|
use Illuminate\Support\Str;
|
|
|
|
class CompareController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$compareIds = $this->compareIds();
|
|
$position = array_flip($compareIds);
|
|
|
|
$products = Product::query()
|
|
->whereIn('id', $compareIds)
|
|
->where('is_active', true)
|
|
->with('category')
|
|
->get()
|
|
->sortBy(fn (Product $product) => $position[$product->id] ?? PHP_INT_MAX)
|
|
->values();
|
|
|
|
$specKeys = $products
|
|
->flatMap(fn (Product $product) => array_keys((array) $product->specs))
|
|
->unique()
|
|
->values();
|
|
|
|
$specLabels = $specKeys->mapWithKeys(fn (string $key) => [$key => $this->specLabel($key)]);
|
|
|
|
return view('shop.compare', [
|
|
'products' => $products,
|
|
'specKeys' => $specKeys,
|
|
'specLabels' => $specLabels,
|
|
]);
|
|
}
|
|
|
|
public function toggle(Product $product)
|
|
{
|
|
$compare = $this->compareIds();
|
|
$exists = in_array($product->id, $compare, true);
|
|
|
|
if ($exists) {
|
|
$compare = array_values(array_filter($compare, fn (int $id) => $id !== $product->id));
|
|
session()->put('compare', $compare);
|
|
|
|
return back()->with('status', "Товар \"{$product->name}\" удален из сравнения.");
|
|
}
|
|
|
|
if (count($compare) >= 4) {
|
|
return back()->with('status', 'Можно сравнить не более 4 товаров одновременно.');
|
|
}
|
|
|
|
$compare[] = $product->id;
|
|
session()->put('compare', array_values(array_unique($compare)));
|
|
|
|
return back()->with('status', "Товар \"{$product->name}\" добавлен в сравнение.");
|
|
}
|
|
|
|
public function clear()
|
|
{
|
|
session()->forget('compare');
|
|
|
|
return back()->with('status', 'Список сравнения очищен.');
|
|
}
|
|
|
|
private function compareIds(): array
|
|
{
|
|
return array_values(array_map('intval', (array) session()->get('compare', [])));
|
|
}
|
|
|
|
private function specLabel(string $key): string
|
|
{
|
|
return match ($key) {
|
|
'manufacturer' => 'Производитель',
|
|
'socket_type' => 'Тип сокета',
|
|
'cpu_type' => 'Тип процессора',
|
|
'form_factor' => 'Форм-фактор',
|
|
'cpu_socket' => 'Сокет для процессора',
|
|
'condition' => 'Состояние',
|
|
'chipset' => 'Чипсет',
|
|
'memory_type' => 'Тип памяти',
|
|
'pcie_version' => 'Версия PCI Express',
|
|
'wifi_standard' => 'Стандарт Wi-Fi',
|
|
'max_memory' => 'Максимальный объем памяти',
|
|
'cache' => 'Объем кэша',
|
|
'capacity' => 'Объем',
|
|
'gpu' => 'GPU',
|
|
'vram' => 'Объем видеопамяти',
|
|
'vram_type' => 'Тип видеопамяти',
|
|
'kit' => 'Количество модулей',
|
|
'frequency' => 'Частота',
|
|
'power' => 'Мощность',
|
|
'efficiency' => 'Сертификат 80 Plus',
|
|
'size' => 'Типоразмер',
|
|
'gpu_length' => 'Макс. длина видеокарты',
|
|
'intel_socket' => 'Сокет Intel',
|
|
'amd_socket' => 'Сокет AMD',
|
|
'fan_speed' => 'Скорость вращения',
|
|
'fans' => 'Количество вентиляторов',
|
|
'type' => 'Тип',
|
|
'model' => 'Модель',
|
|
'color' => 'Цвет',
|
|
'screen_size' => 'Диагональ экрана',
|
|
'cpu_brand' => 'Производитель процессора',
|
|
'cpu_model' => 'Модель процессора',
|
|
'ram' => 'Оперативная память',
|
|
'storage' => 'Накопитель',
|
|
'panel' => 'Тип матрицы',
|
|
'resolution' => 'Разрешение экрана',
|
|
'refresh_rate' => 'Частота обновления',
|
|
'smart_tv' => 'Smart TV',
|
|
'cores' => 'Количество ядер',
|
|
'gpu_brand' => 'Производитель видеокарты',
|
|
'gpu_model' => 'Модель видеокарты',
|
|
default => Str::of($key)->replace('_', ' ')->title()->toString(),
|
|
};
|
|
}
|
|
}
|