Files
ladill-woo-manager/app/Http/Controllers/Woo/CategoryController.php
T
isaaccladandCursor cebf0d2e61
Deploy Ladill Woo Manager / deploy (push) Successful in 51s
Add Woo Manager Pro with multi-store switching and free-tier limits.
Free: 1 store and 20 products. Pro/Business mirrors Invoice pricing (GHS 49/149)
with wallet renewals, Paystack prepay, session-based store switcher, and scoped UI.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-07 01:14:08 +00:00

125 lines
4.0 KiB
PHP

<?php
namespace App\Http\Controllers\Woo;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Woo\Concerns\ResolvesWooContext;
use App\Models\WooCategory;
use App\Services\Woo\CatalogSyncService;
use App\Services\Woo\CatalogWriteService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class CategoryController extends Controller
{
use ResolvesWooContext;
public function __construct(
private CatalogWriteService $write,
private CatalogSyncService $sync,
) {}
public function index(Request $request): View
{
$store = $this->currentStore($request);
$search = trim((string) $request->query('q', ''));
$categories = WooCategory::query()
->when($store, fn ($q) => $q->where('woo_store_id', $store->id))
->when(! $store, fn ($q) => $q->whereRaw('1 = 0'))
->when($search !== '', fn ($q) => $q->where('name', 'like', '%'.$search.'%'))
->with('store')
->orderBy('name')
->paginate(30)
->withQueryString();
return view('woo.categories.index', compact('categories', 'search', 'store'));
}
public function create(Request $request): View
{
$store = $this->currentStoreOrFail($request);
return view('woo.categories.form', [
'category' => new WooCategory(['woo_store_id' => $store->id]),
'currentStore' => $store,
'parentCategories' => $store->categories()->orderBy('name')->get(),
]);
}
public function store(Request $request): RedirectResponse
{
$store = $this->currentStoreOrFail($request);
$validated = $this->validateCategory($request, $store);
$this->write->createCategory($store, $validated);
return redirect()->route('woo.categories.index')
->with('success', 'Category saved.');
}
public function edit(Request $request, WooCategory $category): View
{
$store = $this->currentStoreOrFail($request);
abort_if($category->woo_store_id !== $store->id, 404);
return view('woo.categories.form', [
'category' => $category,
'currentStore' => $store,
'parentCategories' => $store->categories()
->where('external_id', '!=', $category->external_id)
->orderBy('name')
->get(),
]);
}
public function update(Request $request, WooCategory $category): RedirectResponse
{
$store = $this->currentStoreOrFail($request);
abort_if($category->woo_store_id !== $store->id, 404);
$validated = $this->validateCategory($request, $store);
$this->write->updateCategory($category, $validated);
return redirect()->route('woo.categories.index')
->with('success', 'Category updated.');
}
public function destroy(Request $request, WooCategory $category): RedirectResponse
{
$store = $this->currentStoreOrFail($request);
abort_if($category->woo_store_id !== $store->id, 404);
$this->write->deleteCategory($category);
return redirect()->route('woo.categories.index')
->with('success', 'Category removed.');
}
public function sync(Request $request): RedirectResponse
{
$store = $this->currentStoreOrFail($request);
$count = $this->sync->syncCategories($store);
return back()->with('success', sprintf(
'Synced %d categories from %s.',
$count,
$store->site_name ?? $store->site_url,
));
}
/** @return array<string, mixed> */
private function validateCategory(Request $request, $store): array
{
return array_merge($request->validate([
'name' => ['required', 'string', 'max:255'],
'slug' => ['nullable', 'string', 'max:255'],
'description' => ['nullable', 'string', 'max:10000'],
'parent_external_id' => ['nullable', 'integer', 'min:0'],
]), [
'woo_store_id' => $store->id,
]);
}
}