feat: complete admin CMS for customizable layout, texts, products, categories and orders
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of categories.
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
$categories = Category::withCount('products')->get();
|
||||
|
||||
return view('admin.categories.index', compact('categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the category.
|
||||
*/
|
||||
public function edit(Category $category): View
|
||||
{
|
||||
return view('admin.categories.edit', compact('category'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the category in storage.
|
||||
*/
|
||||
public function update(Request $request, Category $category): RedirectResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'icon' => 'nullable|string|max:50',
|
||||
'sort_order' => 'nullable|integer',
|
||||
]);
|
||||
|
||||
$category->update($validated);
|
||||
|
||||
return redirect()->route('admin.categories.index')
|
||||
->with('success', "Categorie '{$category->name}' is succesvol bijgewerkt!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ContentController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the layout and texts editor.
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
return view('admin.content.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Save updated settings and layout options.
|
||||
*/
|
||||
public function update(Request $request): RedirectResponse
|
||||
{
|
||||
$fields = [
|
||||
// Brand & Theme
|
||||
'site_name',
|
||||
'site_tagline',
|
||||
'meta_description',
|
||||
'theme_palette',
|
||||
|
||||
// Announcement
|
||||
'announcement_enabled',
|
||||
'announcement_text',
|
||||
'announcement_badge_1',
|
||||
'announcement_badge_2',
|
||||
|
||||
// Hero
|
||||
'hero_badge',
|
||||
'hero_title',
|
||||
'hero_subtitle',
|
||||
'hero_primary_btn_text',
|
||||
'hero_primary_btn_url',
|
||||
'hero_secondary_btn_text',
|
||||
'hero_secondary_btn_url',
|
||||
'hero_stat_1_val',
|
||||
'hero_stat_1_lbl',
|
||||
'hero_stat_2_val',
|
||||
'hero_stat_2_lbl',
|
||||
'hero_stat_3_val',
|
||||
'hero_stat_3_lbl',
|
||||
'hero_card_badge',
|
||||
|
||||
// Categories Section
|
||||
'categories_tag',
|
||||
'categories_title',
|
||||
'categories_subtitle',
|
||||
|
||||
// Featured Products Section
|
||||
'featured_tag',
|
||||
'featured_title',
|
||||
'featured_subtitle',
|
||||
|
||||
// USP / Quality Section
|
||||
'usp_tag',
|
||||
'usp_title',
|
||||
'usp_subtitle',
|
||||
'usp_1_title',
|
||||
'usp_1_desc',
|
||||
'usp_2_title',
|
||||
'usp_2_desc',
|
||||
'usp_3_title',
|
||||
'usp_3_desc',
|
||||
'usp_4_title',
|
||||
'usp_4_desc',
|
||||
'usp_btn_text',
|
||||
|
||||
// Footer
|
||||
'footer_description',
|
||||
'footer_status_text',
|
||||
'footer_email',
|
||||
'footer_shipping_text',
|
||||
'footer_copyright',
|
||||
'footer_payment_text',
|
||||
];
|
||||
|
||||
// Handle checkbox toggles that aren't present if unchecked
|
||||
if (! $request->has('announcement_enabled')) {
|
||||
Setting::set('announcement_enabled', '0');
|
||||
}
|
||||
|
||||
foreach ($fields as $field) {
|
||||
if ($request->has($field)) {
|
||||
$value = $request->input($field);
|
||||
Setting::set($field, is_string($value) ? trim($value) : $value);
|
||||
}
|
||||
}
|
||||
|
||||
Setting::flushCache();
|
||||
|
||||
return redirect()->route('admin.content.index')
|
||||
->with('success', 'Layout en teksten zijn direct opgeslagen en live bijgewerkt in de webshop!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset specific settings or all back to default.
|
||||
*/
|
||||
public function reset(): RedirectResponse
|
||||
{
|
||||
$keys = [
|
||||
'site_name', 'site_tagline', 'meta_description', 'theme_palette',
|
||||
'announcement_enabled', 'announcement_text', 'announcement_badge_1', 'announcement_badge_2',
|
||||
'hero_badge', 'hero_title', 'hero_subtitle', 'hero_primary_btn_text', 'hero_primary_btn_url',
|
||||
'hero_secondary_btn_text', 'hero_secondary_btn_url',
|
||||
'hero_stat_1_val', 'hero_stat_1_lbl', 'hero_stat_2_val', 'hero_stat_2_lbl', 'hero_stat_3_val', 'hero_stat_3_lbl',
|
||||
'hero_card_badge',
|
||||
'categories_tag', 'categories_title', 'categories_subtitle',
|
||||
'featured_tag', 'featured_title', 'featured_subtitle',
|
||||
'usp_tag', 'usp_title', 'usp_subtitle',
|
||||
'usp_1_title', 'usp_1_desc', 'usp_2_title', 'usp_2_desc', 'usp_3_title', 'usp_3_desc', 'usp_4_title', 'usp_4_desc',
|
||||
'usp_btn_text',
|
||||
'footer_description', 'footer_status_text', 'footer_email', 'footer_shipping_text', 'footer_copyright', 'footer_payment_text',
|
||||
];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
Setting::forget($key);
|
||||
}
|
||||
|
||||
Setting::flushCache();
|
||||
|
||||
return redirect()->route('admin.content.index')
|
||||
->with('success', 'Alle teksten en lay-out instellingen zijn hersteld naar de standaarden.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Order;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class OrderController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of orders.
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$query = Order::with('items')->latest();
|
||||
|
||||
if ($request->filled('status')) {
|
||||
$query->where('status', $request->input('status'));
|
||||
}
|
||||
|
||||
$orders = $query->paginate(20)->withQueryString();
|
||||
|
||||
return view('admin.orders.index', compact('orders'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display order details.
|
||||
*/
|
||||
public function show(Order $order): View
|
||||
{
|
||||
$order->load('items.product');
|
||||
|
||||
return view('admin.orders.show', compact('order'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update order status.
|
||||
*/
|
||||
public function updateStatus(Request $request, Order $order): RedirectResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'status' => 'required|string|in:pending,processing,printed,shipped,completed,cancelled',
|
||||
]);
|
||||
|
||||
$order->update(['status' => $validated['status']]);
|
||||
|
||||
return redirect()->route('admin.orders.show', $order)
|
||||
->with('success', "Bestelling #{$order->order_number} status is gewijzigd naar '{$validated['status']}'!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Category;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of products.
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$query = Product::with('category')->latest();
|
||||
|
||||
if ($request->filled('q')) {
|
||||
$q = $request->input('q');
|
||||
$query->where(function ($w) use ($q) {
|
||||
$w->where('name', 'like', "%{$q}%")
|
||||
->orWhere('description', 'like', "%{$q}%");
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->filled('category_id')) {
|
||||
$query->where('category_id', $request->input('category_id'));
|
||||
}
|
||||
|
||||
$products = $query->paginate(20)->withQueryString();
|
||||
$categories = Category::all();
|
||||
|
||||
return view('admin.products.index', compact('products', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new product.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
$categories = Category::all();
|
||||
|
||||
return view('admin.products.create', compact('categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created product in storage.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
'base_price' => 'required|numeric|min:0.01',
|
||||
'short_description' => 'nullable|string|max:500',
|
||||
'description' => 'required|string',
|
||||
'image_url' => 'nullable|string|max:500',
|
||||
'dimensions' => 'nullable|string|max:100',
|
||||
'weight_grams' => 'nullable|integer|min:0',
|
||||
'print_time_hours' => 'nullable|numeric|min:0',
|
||||
'is_featured' => 'nullable|boolean',
|
||||
'is_active' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
$validated['slug'] = Str::slug($validated['name']);
|
||||
$validated['is_featured'] = $request->boolean('is_featured');
|
||||
$validated['is_active'] = $request->boolean('is_active');
|
||||
$validated['available_materials'] = ['pla', 'petg', 'silk', 'tpu'];
|
||||
$validated['available_colors'] = [
|
||||
'Zon Geel (Pikachu)', 'Vurig Rood (Charizard)', 'Hemelsblauw (Squirtle)',
|
||||
'Muntgroen (Bulbasaur)', 'Spooky Paars (Gengar)', 'Koper Zijdeglans',
|
||||
'Regenboog Multi-Color', 'Mat Zwart', 'Sneeuwwit',
|
||||
];
|
||||
|
||||
Product::create($validated);
|
||||
|
||||
return redirect()->route('admin.products.index')
|
||||
->with('success', "Product '{$validated['name']}' is succesvol aangemaakt!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified product.
|
||||
*/
|
||||
public function edit(Product $product): View
|
||||
{
|
||||
$categories = Category::all();
|
||||
|
||||
return view('admin.products.edit', compact('product', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified product in storage.
|
||||
*/
|
||||
public function update(Request $request, Product $product): RedirectResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
'base_price' => 'required|numeric|min:0.01',
|
||||
'short_description' => 'nullable|string|max:500',
|
||||
'description' => 'required|string',
|
||||
'image_url' => 'nullable|string|max:500',
|
||||
'dimensions' => 'nullable|string|max:100',
|
||||
'weight_grams' => 'nullable|integer|min:0',
|
||||
'print_time_hours' => 'nullable|numeric|min:0',
|
||||
'is_featured' => 'nullable|boolean',
|
||||
'is_active' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
$validated['is_featured'] = $request->boolean('is_featured');
|
||||
$validated['is_active'] = $request->boolean('is_active');
|
||||
|
||||
$product->update($validated);
|
||||
|
||||
return redirect()->route('admin.products.index')
|
||||
->with('success', "Product '{$product->name}' is succesvol bijgewerkt!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified product from storage.
|
||||
*/
|
||||
public function destroy(Product $product): RedirectResponse
|
||||
{
|
||||
$name = $product->name;
|
||||
$product->delete();
|
||||
|
||||
return redirect()->route('admin.products.index')
|
||||
->with('success', "Product '{$name}' is verwijderd.");
|
||||
}
|
||||
}
|
||||
+36
-4
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Throwable;
|
||||
|
||||
class Setting extends Model
|
||||
{
|
||||
@@ -17,23 +18,50 @@ class Setting extends Model
|
||||
'value',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array<string, mixed>|null
|
||||
*/
|
||||
protected static ?array $runtimeCache = null;
|
||||
|
||||
public static function flushCache(): void
|
||||
{
|
||||
static::$runtimeCache = null;
|
||||
}
|
||||
|
||||
public static function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
$setting = static::query()->find($key);
|
||||
if (static::$runtimeCache === null) {
|
||||
try {
|
||||
static::$runtimeCache = static::query()->pluck('value', 'key')->all();
|
||||
} catch (Throwable) {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
if (! $setting || $setting->value === null) {
|
||||
if (! array_key_exists($key, static::$runtimeCache) || static::$runtimeCache[$key] === null) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$decoded = json_decode($setting->value, true);
|
||||
$val = static::$runtimeCache[$key];
|
||||
|
||||
return (json_last_error() === JSON_ERROR_NONE) ? $decoded : $setting->value;
|
||||
if (is_string($val) && (str_starts_with($val, '{') || str_starts_with($val, '['))) {
|
||||
$decoded = json_decode($val, true);
|
||||
if (json_last_error() === JSON_ERROR_NONE) {
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
return $val;
|
||||
}
|
||||
|
||||
public static function set(string $key, mixed $value): static
|
||||
{
|
||||
$encoded = is_array($value) || is_object($value) ? json_encode($value) : (string) $value;
|
||||
|
||||
if (static::$runtimeCache !== null) {
|
||||
static::$runtimeCache[$key] = $encoded;
|
||||
}
|
||||
|
||||
return static::query()->updateOrCreate(
|
||||
['key' => $key],
|
||||
['value' => $encoded]
|
||||
@@ -42,6 +70,10 @@ class Setting extends Model
|
||||
|
||||
public static function forget(string $key): bool
|
||||
{
|
||||
if (static::$runtimeCache !== null) {
|
||||
unset(static::$runtimeCache[$key]);
|
||||
}
|
||||
|
||||
return (bool) static::query()->where('key', $key)->delete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
require_once app_path('helpers.php');
|
||||
|
||||
if (app()->environment('production') || str_starts_with(config('app.url', ''), 'https://')) {
|
||||
URL::forceScheme('https');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Setting;
|
||||
|
||||
if (! function_exists('setting')) {
|
||||
function setting(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return Setting::get($key, $default);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user