Add Campaigns module for grouping QR codes.
Deploy Ladill QR Plus / deploy (push) Successful in 1m6s

Let accounts organise codes by promo or launch, attach or detach codes, and view combined scan totals from the sidebar.
This commit is contained in:
isaacclad
2026-07-16 20:43:02 +00:00
parent ccb7c971e6
commit 95d73d1367
13 changed files with 699 additions and 1 deletions
@@ -0,0 +1,156 @@
<?php
namespace App\Http\Controllers\Qr;
use App\Http\Controllers\Controller;
use App\Models\Campaign;
use App\Models\QrCode;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\View\View;
class CampaignController extends Controller
{
public function index(Request $request): View
{
$account = ladill_account();
$campaigns = Campaign::query()
->where('user_id', $account->id)
->withCount('qrCodes')
->withSum('qrCodes', 'scans_total')
->latest()
->paginate(20)
->withQueryString();
$base = Campaign::query()->where('user_id', $account->id);
return view('qr.campaigns.index', [
'campaigns' => $campaigns,
'campaignCount' => (clone $base)->count(),
'activeCount' => (clone $base)->where('status', Campaign::STATUS_ACTIVE)->count(),
'codesInCampaigns' => QrCode::query()
->where('user_id', $account->id)
->whereNotNull('campaign_id')
->count(),
]);
}
public function create(): View
{
return view('qr.campaigns.create', [
'campaign' => new Campaign(['status' => Campaign::STATUS_ACTIVE]),
]);
}
public function store(Request $request): RedirectResponse
{
$account = ladill_account();
$data = $this->validated($request);
$campaign = Campaign::create([
...$data,
'user_id' => $account->id,
]);
return redirect()
->route('qr.campaigns.show', $campaign)
->with('success', 'Campaign created.');
}
public function show(Campaign $campaign): View
{
$this->authorizeCampaign($campaign);
$codes = $campaign->qrCodes()->latest()->get();
$availableCodes = QrCode::query()
->where('user_id', $campaign->user_id)
->where(function ($q) use ($campaign) {
$q->whereNull('campaign_id')->orWhere('campaign_id', $campaign->id);
})
->orderBy('label')
->get();
return view('qr.campaigns.show', [
'campaign' => $campaign,
'codes' => $codes,
'availableCodes' => $availableCodes,
'totalScans' => (int) $codes->sum('scans_total'),
]);
}
public function update(Request $request, Campaign $campaign): RedirectResponse
{
$this->authorizeCampaign($campaign);
$campaign->update($this->validated($request));
return back()->with('success', 'Campaign updated.');
}
public function destroy(Campaign $campaign): RedirectResponse
{
$this->authorizeCampaign($campaign);
QrCode::query()
->where('campaign_id', $campaign->id)
->update(['campaign_id' => null]);
$campaign->delete();
return redirect()
->route('qr.campaigns.index')
->with('success', 'Campaign deleted. QR codes were unassigned, not removed.');
}
public function attach(Request $request, Campaign $campaign): RedirectResponse
{
$this->authorizeCampaign($campaign);
$validated = $request->validate([
'qr_code_ids' => ['required', 'array', 'min:1'],
'qr_code_ids.*' => ['integer'],
]);
QrCode::query()
->where('user_id', $campaign->user_id)
->whereIn('id', $validated['qr_code_ids'])
->update(['campaign_id' => $campaign->id]);
return back()->with('success', 'QR codes added to campaign.');
}
public function detach(Campaign $campaign, QrCode $qrCode): RedirectResponse
{
$this->authorizeCampaign($campaign);
abort_unless((int) $qrCode->user_id === (int) $campaign->user_id, 403);
abort_unless((int) $qrCode->campaign_id === (int) $campaign->id, 404);
$qrCode->update(['campaign_id' => null]);
return back()->with('success', 'QR code removed from campaign.');
}
/** @return array<string, mixed> */
private function validated(Request $request): array
{
$data = $request->validate([
'name' => ['required', 'string', 'max:120'],
'description' => ['nullable', 'string', 'max:2000'],
'status' => ['required', Rule::in(array_keys(Campaign::STATUSES))],
'starts_at' => ['nullable', 'date'],
'ends_at' => ['nullable', 'date', 'after_or_equal:starts_at'],
]);
$data['description'] = $data['description'] ?? null;
$data['starts_at'] = $data['starts_at'] ?? null;
$data['ends_at'] = $data['ends_at'] ?? null;
return $data;
}
private function authorizeCampaign(Campaign $campaign): void
{
abort_unless((int) $campaign->user_id === (int) ladill_account()->id, 403);
}
}
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Campaign extends Model
{
public const STATUS_DRAFT = 'draft';
public const STATUS_ACTIVE = 'active';
public const STATUS_ARCHIVED = 'archived';
public const STATUSES = [
self::STATUS_DRAFT => 'Draft',
self::STATUS_ACTIVE => 'Active',
self::STATUS_ARCHIVED => 'Archived',
];
protected $fillable = [
'user_id',
'name',
'description',
'status',
'starts_at',
'ends_at',
];
protected function casts(): array
{
return [
'starts_at' => 'datetime',
'ends_at' => 'datetime',
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function qrCodes(): HasMany
{
return $this->hasMany(QrCode::class);
}
public function statusLabel(): string
{
return self::STATUSES[$this->status] ?? ucfirst((string) $this->status);
}
public function isLive(): bool
{
if ($this->status !== self::STATUS_ACTIVE) {
return false;
}
if ($this->starts_at && $this->starts_at->isFuture()) {
return false;
}
if ($this->ends_at && $this->ends_at->isPast()) {
return false;
}
return true;
}
public function totalScans(): int
{
return (int) $this->qrCodes()->sum('scans_total');
}
public function codesCount(): int
{
return (int) $this->qrCodes()->count();
}
}
+6
View File
@@ -30,6 +30,7 @@ class QrCode extends Model
protected $fillable = [ protected $fillable = [
'user_id', 'user_id',
'campaign_id',
'short_code', 'short_code',
'type', 'type',
'label', 'label',
@@ -59,6 +60,11 @@ class QrCode extends Model
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
public function campaign(): BelongsTo
{
return $this->belongsTo(Campaign::class);
}
public function document(): BelongsTo public function document(): BelongsTo
{ {
return $this->belongsTo(QrDocument::class, 'qr_document_id'); return $this->belongsTo(QrDocument::class, 'qr_document_id');
+5
View File
@@ -58,6 +58,11 @@ class User extends Authenticatable
return $this->hasMany(QrCode::class); return $this->hasMany(QrCode::class);
} }
public function campaigns(): HasMany
{
return $this->hasMany(Campaign::class);
}
public function qrSetting(): HasOne public function qrSetting(): HasOne
{ {
return $this->hasOne(QrSetting::class); return $this->hasOne(QrSetting::class);
+2 -1
View File
@@ -106,6 +106,7 @@ class AfiaService
- My Codes: list, create, and edit QR codes. Types: Link (URL), PDF, List of Links, Business profile, WiFi, App download. - My Codes: list, create, and edit QR codes. Types: Link (URL), PDF, List of Links, Business profile, WiFi, App download.
- Creating a code: pick a type, set content, customize style (colors, logo, frame), then download PNG/SVG/PDF. - Creating a code: pick a type, set content, customize style (colors, logo, frame), then download PNG/SVG/PDF.
- Dynamic codes: destination can change after printing the printed QR always points to ladill.com/q/{code}. - Dynamic codes: destination can change after printing the printed QR always points to ladill.com/q/{code}.
- Campaigns: group QR codes for a promo or launch (sidebar Campaigns), attach/detach codes, and see combined scan totals.
- Business QR: name, tagline, contact, hours, logo, cover, social links shown as a mobile landing page. - Business QR: name, tagline, contact, hours, logo, cover, social links shown as a mobile landing page.
- WiFi QR: guests scan to join; network name and password are encoded in the code. - WiFi QR: guests scan to join; network name and password are encoded in the code.
- PDF QR: hosts a PDF with optional download button on the viewer. - PDF QR: hosts a PDF with optional download button on the viewer.
@@ -114,7 +115,7 @@ class AfiaService
- Developers: API tokens for programmatic code creation (sidebar Developers). - Developers: API tokens for programmatic code creation (sidebar Developers).
Rules: Rules:
- Only answer questions about Ladill QR Plus code types, styling, downloads, scans, wallet billing, team, and API. - Only answer questions about Ladill QR Plus code types, styling, downloads, campaigns, scans, wallet billing, team, and API.
- If asked about domains, hosting, email, or payments/commerce QR (shop, events, donations), briefly say those live in other Ladill apps and suggest the app launcher. - If asked about domains, hosting, email, or payments/commerce QR (shop, events, donations), briefly say those live in other Ladill apps and suggest the app launcher.
- Never invent prices, short codes, or wallet balances use the user context below or tell them where to check. - Never invent prices, short codes, or wallet balances use the user context below or tell them where to check.
- For print tips: recommend high contrast, adequate quiet zone, test-scan before mass printing. - For print tips: recommend high contrast, adequate quiet zone, test-scan before mass printing.
@@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('campaigns', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('name');
$table->text('description')->nullable();
$table->string('status', 20)->default('active')->index();
$table->timestamp('starts_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamps();
$table->index(['user_id', 'status']);
});
Schema::table('qr_codes', function (Blueprint $table) {
$table->foreignId('campaign_id')->nullable()->after('user_id')->constrained('campaigns')->nullOnDelete();
});
}
public function down(): void
{
Schema::table('qr_codes', function (Blueprint $table) {
$table->dropConstrainedForeignId('campaign_id');
});
Schema::dropIfExists('campaigns');
}
};
@@ -10,6 +10,8 @@
'icon' => '<path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12 11.2 3.05c.44-.44 1.15-.44 1.59 0L21.75 12M4.5 9.75v10.5a.75.75 0 0 0 .75.75H9.75v-6a.75.75 0 0 1 .75-.75h3a.75.75 0 0 1 .75.75v6h4.5a.75.75 0 0 0 .75-.75V9.75" />'], 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12 11.2 3.05c.44-.44 1.15-.44 1.59 0L21.75 12M4.5 9.75v10.5a.75.75 0 0 0 .75.75H9.75v-6a.75.75 0 0 1 .75-.75h3a.75.75 0 0 1 .75.75v6h4.5a.75.75 0 0 0 .75-.75V9.75" />'],
['name' => 'My Codes', 'route' => route('user.qr-codes.index'), 'active' => request()->routeIs('user.qr-codes.*'), ['name' => 'My Codes', 'route' => route('user.qr-codes.index'), 'active' => request()->routeIs('user.qr-codes.*'),
'icon' => '<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 4.875c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5A1.125 1.125 0 0 1 3.75 9.375v-4.5ZM3.75 14.625c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5a1.125 1.125 0 0 1-1.125-1.125v-4.5ZM13.5 4.875c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5A1.125 1.125 0 0 1 13.5 9.375v-4.5Z" /><path stroke-linecap="round" stroke-linejoin="round" d="M6.75 6.75h.75v.75h-.75v-.75ZM6.75 16.5h.75v.75h-.75v-.75ZM16.5 6.75h.75v.75h-.75v-.75ZM13.5 13.5h.75v.75h-.75v-.75ZM16.5 16.5h.75v.75h-.75v-.75ZM13.5 19.5h.75v.75h-.75v-.75ZM19.5 13.5h.75v.75h-.75v-.75ZM19.5 19.5h.75v.75h-.75v-.75ZM22.5 18.75a2.25 2.25 0 0 0-2.25-2.25h-1.5a2.25 2.25 0 0 0-2.25 2.25v1.5a2.25 2.25 0 0 0 2.25 2.25h1.5a2.25 2.25 0 0 0 2.25-2.25v-1.5Z" />'], 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 4.875c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5A1.125 1.125 0 0 1 3.75 9.375v-4.5ZM3.75 14.625c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5a1.125 1.125 0 0 1-1.125-1.125v-4.5ZM13.5 4.875c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5A1.125 1.125 0 0 1 13.5 9.375v-4.5Z" /><path stroke-linecap="round" stroke-linejoin="round" d="M6.75 6.75h.75v.75h-.75v-.75ZM6.75 16.5h.75v.75h-.75v-.75ZM16.5 6.75h.75v.75h-.75v-.75ZM13.5 13.5h.75v.75h-.75v-.75ZM16.5 16.5h.75v.75h-.75v-.75ZM13.5 19.5h.75v.75h-.75v-.75ZM19.5 13.5h.75v.75h-.75v-.75ZM19.5 19.5h.75v.75h-.75v-.75ZM22.5 18.75a2.25 2.25 0 0 0-2.25-2.25h-1.5a2.25 2.25 0 0 0-2.25 2.25v1.5a2.25 2.25 0 0 0 2.25 2.25h1.5a2.25 2.25 0 0 0 2.25-2.25v-1.5Z" />'],
['name' => 'Campaigns', 'route' => route('qr.campaigns.index'), 'active' => request()->routeIs('qr.campaigns.*'),
'icon' => '<path stroke-linecap="round" stroke-linejoin="round" d="M10.34 15.84c-.688-.06-1.386-.09-2.09-.09H7.5a4.5 4.5 0 1 1 0-9h.75c.704 0 1.402-.03 2.09-.09m0 9.18c.253.962.584 1.892.985 2.783.247.55.06 1.21-.463 1.511l-.657.38c-.551.318-1.26.117-1.527-.461a20.845 20.845 0 0 1-1.44-4.282m3.102.069a18.03 18.03 0 0 1-.59-4.59c0-1.586.205-3.124.59-4.59m0 9.18a23.848 23.848 0 0 1 8.789 2.553c.56.246 1.198-.004 1.434-.56a11.958 11.958 0 0 0 1.15-4.506c.02-.292.02-.585 0-.877a11.958 11.958 0 0 0-1.15-4.506c-.236-.556-.874-.806-1.434-.56a23.848 23.848 0 0 1-8.789 2.553m0 0a23.948 23.948 0 0 0 0 5.634" />'],
['name' => 'Analytics', 'route' => route('qr.analytics.index'), 'active' => request()->routeIs('qr.analytics.*'), ['name' => 'Analytics', 'route' => route('qr.analytics.index'), 'active' => request()->routeIs('qr.analytics.*'),
'icon' => '<path stroke-linecap="round" stroke-linejoin="round" d="M3 13.125C3 12.504 3.504 12 4.125 12h2.25c.621 0 1.125.504 1.125 1.125v6.75C7.5 20.496 6.996 21 6.375 21h-2.25A1.125 1.125 0 0 1 3 19.875v-6.75ZM9.75 8.625c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125v11.25c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 0 1-1.125-1.125V8.625ZM16.5 4.125c0-.621.504-1.125 1.125-1.125h2.25C20.496 3 21 3.504 21 4.125v15.75c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 0 1-1.125-1.125V4.125Z" />'], 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" d="M3 13.125C3 12.504 3.504 12 4.125 12h2.25c.621 0 1.125.504 1.125 1.125v6.75C7.5 20.496 6.996 21 6.375 21h-2.25A1.125 1.125 0 0 1 3 19.875v-6.75ZM9.75 8.625c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125v11.25c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 0 1-1.125-1.125V8.625ZM16.5 4.125c0-.621.504-1.125 1.125-1.125h2.25C20.496 3 21 3.504 21 4.125v15.75c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 0 1-1.125-1.125V4.125Z" />'],
]; ];
@@ -0,0 +1,42 @@
<div>
<label for="name" class="block text-sm font-medium text-slate-700">Name</label>
<input type="text" name="name" id="name" required value="{{ old('name', $campaign->name) }}"
class="mt-1 block w-full rounded-lg border-slate-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
placeholder="e.g. Store opening · April">
@error('name')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
</div>
<div>
<label for="description" class="block text-sm font-medium text-slate-700">Description <span class="font-normal text-slate-400">(optional)</span></label>
<textarea name="description" id="description" rows="3"
class="mt-1 block w-full rounded-lg border-slate-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
placeholder="What this campaign is for">{{ old('description', $campaign->description) }}</textarea>
@error('description')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
</div>
<div>
<label for="status" class="block text-sm font-medium text-slate-700">Status</label>
<select name="status" id="status" class="mt-1 block w-full rounded-lg border-slate-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
@foreach (\App\Models\Campaign::STATUSES as $value => $label)
<option value="{{ $value }}" @selected(old('status', $campaign->status ?? 'active') === $value)>{{ $label }}</option>
@endforeach
</select>
@error('status')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
</div>
<div class="grid gap-4 sm:grid-cols-2">
<div>
<label for="starts_at" class="block text-sm font-medium text-slate-700">Starts <span class="font-normal text-slate-400">(optional)</span></label>
<input type="date" name="starts_at" id="starts_at"
value="{{ old('starts_at', optional($campaign->starts_at)->format('Y-m-d')) }}"
class="mt-1 block w-full rounded-lg border-slate-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
@error('starts_at')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
</div>
<div>
<label for="ends_at" class="block text-sm font-medium text-slate-700">Ends <span class="font-normal text-slate-400">(optional)</span></label>
<input type="date" name="ends_at" id="ends_at"
value="{{ old('ends_at', optional($campaign->ends_at)->format('Y-m-d')) }}"
class="mt-1 block w-full rounded-lg border-slate-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
@error('ends_at')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
</div>
</div>
@@ -0,0 +1,20 @@
<x-user-layout>
<x-slot name="title">New campaign</x-slot>
<div class="mx-auto max-w-xl space-y-6">
<div>
<a href="{{ route('qr.campaigns.index') }}" class="text-sm text-slate-500 hover:text-slate-700">&larr; Campaigns</a>
<h1 class="mt-2 text-2xl font-semibold text-slate-900">New campaign</h1>
<p class="mt-1 text-sm text-slate-500">Name a launch or promo, then attach QR codes on the next screen.</p>
</div>
<form method="POST" action="{{ route('qr.campaigns.store') }}" class="space-y-5 rounded-xl border border-slate-200 bg-white p-6">
@csrf
@include('qr.campaigns._form', ['campaign' => $campaign])
<div class="flex justify-end gap-3">
<a href="{{ route('qr.campaigns.index') }}" class="rounded-xl px-4 py-2 text-sm font-medium text-slate-600 hover:bg-slate-50">Cancel</a>
<button type="submit" class="btn-primary">Create campaign</button>
</div>
</form>
</div>
</x-user-layout>
@@ -0,0 +1,100 @@
@php
$statusColor = [
'draft' => 'bg-slate-100 text-slate-700',
'active' => 'bg-emerald-50 text-emerald-700',
'archived' => 'bg-amber-50 text-amber-800',
];
@endphp
<x-user-layout>
<x-slot name="title">Campaigns</x-slot>
<div class="space-y-6">
@foreach (['success', 'error'] as $flash)
@if (session($flash))
<div class="rounded-lg border px-4 py-3 {{ $flash === 'success' ? 'border-emerald-200 bg-emerald-50 text-emerald-700' : 'border-red-200 bg-red-50 text-red-700' }}">
<p class="text-sm">{{ session($flash) }}</p>
</div>
@endif
@endforeach
<div class="relative overflow-hidden rounded-2xl border border-slate-200 bg-white">
<div class="absolute inset-0 bg-gradient-to-br from-violet-50/80 via-white to-indigo-50/60"></div>
<div class="relative px-6 py-8 sm:px-10">
<div class="flex flex-col gap-6 lg:flex-row lg:items-center lg:justify-between">
<div class="max-w-xl">
<div class="inline-flex items-center gap-1.5 rounded-full bg-violet-100 px-3 py-1 text-xs font-semibold text-violet-700">
Group codes · Track performance
</div>
<h1 class="mt-3 text-2xl font-bold tracking-tight text-slate-900 sm:text-3xl">Campaigns</h1>
<p class="mt-2 text-sm leading-6 text-slate-600">
Organise QR codes by launch, promo, or channel and see combined scan totals in one place.
</p>
<div class="mt-6">
<a href="{{ route('qr.campaigns.create') }}" class="btn-primary">
<svg class="h-4 w-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15"/></svg>
New campaign
</a>
</div>
</div>
<div class="scrollbar-hide flex snap-x snap-mandatory gap-3 overflow-x-auto pb-2 sm:grid sm:grid-cols-3 sm:overflow-visible sm:pb-0 lg:gap-4" style="-ms-overflow-style:none;scrollbar-width:none;">
<div class="mobile-stats-card shrink-0 snap-start rounded-xl border border-slate-200 bg-white/90 px-4 py-3 text-center backdrop-blur-sm">
<p class="whitespace-nowrap text-xl font-bold text-slate-900 sm:text-2xl">{{ number_format($campaignCount) }}</p>
<p class="mt-0.5 whitespace-nowrap text-[11px] font-medium text-slate-500">Campaigns</p>
</div>
<div class="mobile-stats-card shrink-0 snap-start rounded-xl border border-slate-200 bg-white/90 px-4 py-3 text-center backdrop-blur-sm">
<p class="whitespace-nowrap text-xl font-bold text-slate-900 sm:text-2xl">{{ number_format($activeCount) }}</p>
<p class="mt-0.5 whitespace-nowrap text-[11px] font-medium text-slate-500">Active</p>
</div>
<div class="mobile-stats-card shrink-0 snap-start rounded-xl border border-slate-200 bg-white/90 px-4 py-3 text-center backdrop-blur-sm">
<p class="whitespace-nowrap text-xl font-bold text-slate-900 sm:text-2xl">{{ number_format($codesInCampaigns) }}</p>
<p class="mt-0.5 whitespace-nowrap text-[11px] font-medium text-slate-500">Codes assigned</p>
</div>
</div>
</div>
</div>
</div>
<div class="overflow-hidden rounded-2xl border border-slate-200 bg-white">
<div class="border-b border-slate-100 px-6 py-4">
<h2 class="text-sm font-semibold text-slate-900">All campaigns</h2>
</div>
@if ($campaigns->isEmpty())
<div class="px-6 py-12 text-center">
<p class="text-sm text-slate-500">No campaigns yet. Create one to group QR codes for a promo or launch.</p>
</div>
@else
<table class="min-w-full divide-y divide-slate-100 text-sm">
<thead class="bg-slate-50 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">
<tr>
<th class="px-5 py-3">Campaign</th>
<th class="hidden px-5 py-3 sm:table-cell">Codes</th>
<th class="hidden px-5 py-3 md:table-cell">Scans</th>
<th class="px-5 py-3">Status</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-50">
@foreach ($campaigns as $campaign)
<tr class="cursor-pointer hover:bg-slate-50" onclick="window.location='{{ route('qr.campaigns.show', $campaign) }}'">
<td class="px-5 py-3">
<p class="font-medium text-slate-900">{{ $campaign->name }}</p>
@if ($campaign->description)
<p class="mt-0.5 line-clamp-1 text-xs text-slate-500">{{ $campaign->description }}</p>
@endif
</td>
<td class="hidden px-5 py-3 text-slate-600 sm:table-cell">{{ number_format($campaign->qr_codes_count) }}</td>
<td class="hidden px-5 py-3 text-slate-600 md:table-cell">{{ number_format((int) $campaign->qr_codes_sum_scans_total) }}</td>
<td class="px-5 py-3">
<span class="rounded-full px-2 py-0.5 text-xs font-medium {{ $statusColor[$campaign->status] ?? 'bg-slate-100 text-slate-700' }}">
{{ $campaign->statusLabel() }}
</span>
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="border-t border-slate-100 px-5 py-3">{{ $campaigns->links() }}</div>
@endif
</div>
</div>
</x-user-layout>
+115
View File
@@ -0,0 +1,115 @@
@php
$statusColor = [
'draft' => 'bg-slate-100 text-slate-700',
'active' => 'bg-emerald-50 text-emerald-700',
'archived' => 'bg-amber-50 text-amber-800',
];
@endphp
<x-user-layout>
<x-slot name="title">{{ $campaign->name }}</x-slot>
<div class="mx-auto max-w-5xl space-y-6">
@foreach (['success', 'error'] as $flash)
@if (session($flash))
<div class="rounded-lg border px-4 py-3 {{ $flash === 'success' ? 'border-emerald-200 bg-emerald-50 text-emerald-700' : 'border-red-200 bg-red-50 text-red-700' }}">
<p class="text-sm">{{ session($flash) }}</p>
</div>
@endif
@endforeach
<div class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
<div>
<a href="{{ route('qr.campaigns.index') }}" class="text-sm text-slate-500 hover:text-slate-700">&larr; Campaigns</a>
<div class="mt-2 flex flex-wrap items-center gap-2">
<h1 class="text-2xl font-semibold text-slate-900">{{ $campaign->name }}</h1>
<span class="rounded-full px-2 py-0.5 text-xs font-medium {{ $statusColor[$campaign->status] ?? 'bg-slate-100 text-slate-700' }}">
{{ $campaign->statusLabel() }}
</span>
</div>
@if ($campaign->description)
<p class="mt-1 text-sm text-slate-600">{{ $campaign->description }}</p>
@endif
</div>
<form method="POST" action="{{ route('qr.campaigns.destroy', $campaign) }}" onsubmit="return confirm('Delete this campaign? Codes stay; they are only unassigned.')">
@csrf @method('DELETE')
<button type="submit" class="text-sm text-red-600 hover:text-red-700">Delete campaign</button>
</form>
</div>
<div class="grid grid-cols-2 gap-4 sm:grid-cols-3">
<div class="rounded-xl border border-slate-200 bg-white p-4">
<p class="text-2xl font-bold text-slate-900">{{ number_format($codes->count()) }}</p>
<p class="mt-0.5 text-xs text-slate-500">QR codes</p>
</div>
<div class="rounded-xl border border-slate-200 bg-white p-4">
<p class="text-2xl font-bold text-slate-900">{{ number_format($totalScans) }}</p>
<p class="mt-0.5 text-xs text-slate-500">Total scans</p>
</div>
<div class="rounded-xl border border-slate-200 bg-white p-4 col-span-2 sm:col-span-1">
<p class="text-sm font-semibold text-slate-900">
@if ($campaign->starts_at || $campaign->ends_at)
{{ optional($campaign->starts_at)->format('d M Y') ?? '…' }}
{{ optional($campaign->ends_at)->format('d M Y') ?? '…' }}
@else
No date range
@endif
</p>
<p class="mt-0.5 text-xs text-slate-500">Schedule</p>
</div>
</div>
<form method="POST" action="{{ route('qr.campaigns.update', $campaign) }}" class="space-y-5 rounded-xl border border-slate-200 bg-white p-6">
@csrf @method('PATCH')
<h2 class="text-base font-semibold text-slate-900">Campaign details</h2>
@include('qr.campaigns._form', ['campaign' => $campaign])
<button type="submit" class="btn-primary">Save changes</button>
</form>
<div class="rounded-xl border border-slate-200 bg-white">
<div class="border-b border-slate-100 px-6 py-4">
<h2 class="text-base font-semibold text-slate-900">QR codes in this campaign</h2>
</div>
@if ($codes->isEmpty())
<p class="px-6 py-8 text-center text-sm text-slate-500">No codes assigned yet.</p>
@else
<ul class="divide-y divide-slate-100">
@foreach ($codes as $code)
<li class="flex items-center justify-between gap-4 px-6 py-3">
<a href="{{ route('user.qr-codes.show', $code) }}" class="min-w-0 flex-1 hover:text-indigo-700">
<p class="truncate text-sm font-medium text-slate-900">{{ $code->label ?: $code->short_code }}</p>
<p class="truncate text-xs text-slate-500">{{ $code->publicUrl() }} · {{ number_format($code->scans_total) }} scans</p>
</a>
<form method="POST" action="{{ route('qr.campaigns.detach', [$campaign, $code]) }}">
@csrf @method('DELETE')
<button type="submit" class="text-xs font-medium text-red-600 hover:text-red-800">Remove</button>
</form>
</li>
@endforeach
</ul>
@endif
</div>
@php
$unassigned = $availableCodes->whereNull('campaign_id');
@endphp
@if ($unassigned->isNotEmpty())
<form method="POST" action="{{ route('qr.campaigns.attach', $campaign) }}" class="space-y-4 rounded-xl border border-slate-200 bg-white p-6">
@csrf
<h2 class="text-base font-semibold text-slate-900">Add QR codes</h2>
<div class="max-h-56 space-y-2 overflow-y-auto rounded-lg border border-slate-100 p-3">
@foreach ($unassigned as $code)
<label class="flex items-center gap-3 rounded-lg px-2 py-1.5 text-sm hover:bg-slate-50">
<input type="checkbox" name="qr_code_ids[]" value="{{ $code->id }}" class="rounded border-slate-300 text-indigo-600 focus:ring-indigo-500">
<span class="min-w-0 flex-1 truncate">
<span class="font-medium text-slate-900">{{ $code->label ?: $code->short_code }}</span>
<span class="text-slate-400"> · {{ $code->short_code }}</span>
</span>
</label>
@endforeach
</div>
<button type="submit" class="btn-primary">Add selected</button>
</form>
@endif
</div>
</x-user-layout>
+10
View File
@@ -9,6 +9,7 @@ use App\Http\Controllers\Qr\AccountController;
use App\Http\Controllers\Qr\AfiaController; use App\Http\Controllers\Qr\AfiaController;
use App\Http\Controllers\Qr\AnalyticsController; use App\Http\Controllers\Qr\AnalyticsController;
use App\Http\Controllers\Qr\DeveloperController; use App\Http\Controllers\Qr\DeveloperController;
use App\Http\Controllers\Qr\CampaignController;
use App\Http\Controllers\Qr\OverviewController; use App\Http\Controllers\Qr\OverviewController;
use App\Http\Controllers\Qr\TeamController; use App\Http\Controllers\Qr\TeamController;
use App\Http\Controllers\Qr\QrCodeController; use App\Http\Controllers\Qr\QrCodeController;
@@ -50,6 +51,15 @@ Route::middleware(['auth', 'platform.session'])->group(function () {
Route::post('/afia/chat', [AfiaController::class, 'chat'])->name('qr.afia.chat'); Route::post('/afia/chat', [AfiaController::class, 'chat'])->name('qr.afia.chat');
Route::get('/search', SearchController::class)->name('qr.search'); Route::get('/search', SearchController::class)->name('qr.search');
Route::get('/campaigns', [CampaignController::class, 'index'])->name('qr.campaigns.index');
Route::get('/campaigns/create', [CampaignController::class, 'create'])->name('qr.campaigns.create');
Route::post('/campaigns', [CampaignController::class, 'store'])->name('qr.campaigns.store');
Route::get('/campaigns/{campaign}', [CampaignController::class, 'show'])->name('qr.campaigns.show');
Route::patch('/campaigns/{campaign}', [CampaignController::class, 'update'])->name('qr.campaigns.update');
Route::delete('/campaigns/{campaign}', [CampaignController::class, 'destroy'])->name('qr.campaigns.destroy');
Route::post('/campaigns/{campaign}/attach', [CampaignController::class, 'attach'])->name('qr.campaigns.attach');
Route::delete('/campaigns/{campaign}/codes/{qrCode}', [CampaignController::class, 'detach'])->name('qr.campaigns.detach');
Route::get('/qr-codes', [QrCodeController::class, 'index'])->name('user.qr-codes.index'); Route::get('/qr-codes', [QrCodeController::class, 'index'])->name('user.qr-codes.index');
Route::get('/qr-codes/create', [QrCodeController::class, 'create'])->name('user.qr-codes.create'); Route::get('/qr-codes/create', [QrCodeController::class, 'create'])->name('user.qr-codes.create');
Route::post('/qr-codes', [QrCodeController::class, 'store'])->name('user.qr-codes.store'); Route::post('/qr-codes', [QrCodeController::class, 'store'])->name('user.qr-codes.store');
+123
View File
@@ -0,0 +1,123 @@
<?php
namespace Tests\Feature;
use App\Http\Middleware\EnsurePlatformSession;
use App\Models\Campaign;
use App\Models\QrCode;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Tests\TestCase;
class CampaignTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->withoutMiddleware(EnsurePlatformSession::class);
if (method_exists($this, 'withoutVite')) {
$this->withoutVite();
}
}
private function user(): User
{
return User::create([
'public_id' => (string) Str::uuid(),
'name' => 'Owner',
'email' => 'owner+'.uniqid().'@example.com',
]);
}
private function code(User $user, array $attrs = []): QrCode
{
return QrCode::create(array_merge([
'user_id' => $user->id,
'short_code' => strtolower(Str::random(8)),
'type' => QrCode::TYPE_URL,
'label' => 'Test code',
'destination_url' => 'https://example.com',
'is_active' => true,
'scans_total' => 5,
'unique_scans_total' => 3,
], $attrs));
}
public function test_owner_can_create_campaign_and_attach_codes(): void
{
$user = $this->user();
$code = $this->code($user);
$this->actingAs($user)
->post(route('qr.campaigns.store'), [
'name' => 'Spring promo',
'description' => 'April flyers',
'status' => Campaign::STATUS_ACTIVE,
])
->assertRedirect();
$campaign = Campaign::first();
$this->assertNotNull($campaign);
$this->assertSame('Spring promo', $campaign->name);
$this->assertSame($user->id, $campaign->user_id);
$this->actingAs($user)
->post(route('qr.campaigns.attach', $campaign), [
'qr_code_ids' => [$code->id],
])
->assertRedirect()
->assertSessionHas('success');
$this->assertSame($campaign->id, $code->fresh()->campaign_id);
$this->actingAs($user)
->get(route('qr.campaigns.show', $campaign))
->assertOk()
->assertSee('Spring promo')
->assertSee($code->short_code);
}
public function test_owner_can_detach_and_delete_campaign(): void
{
$user = $this->user();
$campaign = Campaign::create([
'user_id' => $user->id,
'name' => 'Temp',
'status' => Campaign::STATUS_ACTIVE,
]);
$code = $this->code($user, ['campaign_id' => $campaign->id]);
$this->actingAs($user)
->delete(route('qr.campaigns.detach', [$campaign, $code]))
->assertRedirect();
$this->assertNull($code->fresh()->campaign_id);
$code->update(['campaign_id' => $campaign->id]);
$this->actingAs($user)
->delete(route('qr.campaigns.destroy', $campaign))
->assertRedirect(route('qr.campaigns.index'));
$this->assertDatabaseMissing('campaigns', ['id' => $campaign->id]);
$this->assertNull($code->fresh()->campaign_id);
}
public function test_other_user_cannot_view_campaign(): void
{
$owner = $this->user();
$other = $this->user();
$campaign = Campaign::create([
'user_id' => $owner->id,
'name' => 'Private',
'status' => Campaign::STATUS_ACTIVE,
]);
$this->actingAs($other)
->get(route('qr.campaigns.show', $campaign))
->assertForbidden();
}
}