<?php
 namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Http\Requests\Admin\ProductCategoryDestroyRequest; use App\Http\Requests\Admin\ProductCategoryEditRequest; use App\Http\Requests\Admin\ProductCategoryIndexRequest; use App\Http\Requests\Admin\ProductCategoryRecordsRequest; use App\Http\Requests\Admin\ProductCategoryShowRequest; use App\Http\Requests\Admin\ProductCategoryStoreRequest; use App\Http\Requests\Admin\ProductCategoryUpdateRequest; use App\Models\ProductCategory; use App\Traits\LogsAdminActivity; use Illuminate\Http\Request; use Illuminate\Support\Str; class ProductCategoryController extends Controller { use LogsAdminActivity; public function index(ProductCategoryIndexRequest $request) { return view('admin.product-categories.index'); } public function records(ProductCategoryRecordsRequest $request) { $query = ProductCategory::withCount('products'); if ($request->has('search') && ! empty($request->search['value'])) { $search = $request->search['value']; $query->where(function ($q) use ($search) { $q->where('name', 'like', "%{$search}%") ->orWhere('slug', 'like', "%{$search}%"); }); } if ($request->has('order')) { $column = $request->order[0]['column']; $dir = $request->order[0]['dir']; $columns = [ 0 => 'name', 1 => 'slug', 2 => 'order', 3 => 'is_active', 4 => 'products_count', ]; if (isset($columns[$column])) { if ($columns[$column] === 'products_count') { $query->orderBy('products_count', $dir); } else { $query->orderBy($columns[$column], $dir); } } } else { $query->orderBy('order', 'asc') ->orderBy('name', 'asc'); } $total = $query->count(); $start = $request->input('start', 0); $length = $request->input('length', 10); $query->skip($start)->take($length); $data = $query->get()->map(function ($category) { return [ 'id' => $category->id, 'name' => $category->name, 'slug' => $category->slug, 'icon' => $category->icon, 'order' => $category->order, 'is_active' => $category->is_active, 'products_count' => $category->products_count, ]; }); return response()->json([ 'draw' => $request->input('draw'), 'recordsTotal' => $total, 'recordsFiltered' => $total, 'data' => $data, ]); } public function create() { return redirect()->route('admin.product-categories.index'); } public function store(ProductCategoryStoreRequest $request) { $validated = $request->validated(); if (empty($validated['slug'])) { $validated['slug'] = Str::slug($validated['name']); } else { $validated['slug'] = Str::slug($validated['slug']); } $originalSlug = $validated['slug']; $count = 1; while (ProductCategory::where('slug', $validated['slug'])->exists()) { $validated['slug'] = $originalSlug.'-'.$count++; } $validated['is_active'] = $request->has('is_active') ? 1 : 0; $category = ProductCategory::create($validated); $this->logCreated($category, "Created product category #{$category->id} - {$category->name}"); if ($request->wantsJson() || $request->ajax()) { return response()->json([ 'success' => true, 'message' => __('admin/product-categories.messages_created'), 'data' => $category, ]); } return redirect() ->route('admin.product-categories.index') ->with('success', __('admin/product-categories.messages_created')); } public function show(ProductCategoryShowRequest $request, ProductCategory $productCategory) { return response()->json($productCategory); } public function edit(ProductCategoryEditRequest $request, ProductCategory $productCategory) { return redirect()->route('admin.product-categories.index'); } public function update(ProductCategoryUpdateRequest $request, ProductCategory $productCategory) { $validated = $request->validated(); if (empty($validated['slug'])) { $validated['slug'] = Str::slug($validated['name']); } else { $validated['slug'] = Str::slug($validated['slug']); } if ($validated['slug'] !== $productCategory->slug) { $originalSlug = $validated['slug']; $count = 1; while (ProductCategory::where('slug', $validated['slug']) ->where('id', '!=', $productCategory->id) ->exists()) { $validated['slug'] = $originalSlug.'-'.$count++; } } $validated['is_active'] = $request->has('is_active') ? 1 : 0; $productCategory->update($validated); $this->logUpdated($productCategory, "Updated product category #{$productCategory->id} - {$productCategory->name}"); if ($request->wantsJson() || $request->ajax()) { return response()->json([ 'success' => true, 'message' => __('admin/product-categories.messages_updated'), 'data' => $productCategory, ]); } return redirect() ->route('admin.product-categories.index') ->with('success', __('admin/product-categories.messages_updated')); } public function destroy(ProductCategoryDestroyRequest $request, ProductCategory $productCategory) { $this->logDeleted($productCategory, "Deleted product category #{$productCategory->id} - {$productCategory->name}"); $productCategory->delete(); return response()->json([ 'success' => true, 'message' => __('admin/product-categories.messages_deleted'), ]); } } 