| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Ai\AiException;
- use App\Models\Store;
- use App\Storefront\Logos\StoreLogos;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- use Inertia\Inertia;
- use RuntimeException;
-
- /**
- * The Logo box in the Design tab: upload a logo, pick a ready design, or have Claude draw some.
- * The logo belongs to the store, so a live store shows it right away (no "Publish changes" needed).
- */
- class StoreLogoController extends Controller
- {
- public function upload(Request $request, string $slug, StoreLogos $logos): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- $request->validate([
- 'logo' => [
- 'required', 'image', 'mimes:jpg,jpeg,png,webp', 'max:2048',
- 'dimensions:min_width='.StoreLogos::MIN_SIDE.',min_height='.StoreLogos::MIN_SIDE.',max_width=6000,max_height=6000',
- ],
- ], [
- 'logo.max' => __('The logo must be 2 MB or smaller.'),
- 'logo.mimes' => __('Use a PNG, JPG or WebP picture. PNG keeps a see-through background.'),
- 'logo.dimensions' => __('Use a picture at least :size pixels on each side.', ['size' => StoreLogos::MIN_SIDE]),
- ]);
-
- try {
- $logos->upload($store, $request->file('logo'));
- } catch (RuntimeException) {
- return back()->withErrors(['logo' => __('That picture could not be read. Try saving it as PNG.')]);
- }
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Logo uploaded. It shows on your store now.')]);
-
- return back();
- }
-
- /**
- * Use a ready design, or one of the logos Claude drew.
- */
- public function choose(Request $request, string $slug, StoreLogos $logos): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- $validated = $request->validate([
- 'design' => ['required_without:drawn', 'nullable', Rule::in(StoreLogos::DESIGNS)],
- 'drawn' => ['required_without:design', 'nullable', 'integer', 'min:0'],
- ]);
-
- if (filled($validated['design'] ?? null)) {
- $logos->useDesign($store, $validated['design']);
- } elseif (! $logos->useDrawn($store, (int) $validated['drawn'])) {
- return back()->withErrors(['logo' => __('That logo is no longer available.')]);
- }
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Logo saved. It shows on your store now.')]);
-
- return back();
- }
-
- /**
- * Ask Claude for new logos. Each store has a lifetime number of tries.
- */
- public function draw(Request $request, string $slug, StoreLogos $logos): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- try {
- $made = $logos->draw($store);
- } catch (AiException $e) {
- report($e);
-
- return back()->withErrors(['logo' => __('The AI could not draw logos just now. This try was not counted. Please try again.')]);
- }
-
- if ($made === null) {
- return back()->withErrors(['logo' => __('You have used all :count AI logo tries for this store. You can still pick any logo drawn before, a ready design, or upload your own.', ['count' => $logos->tryLimit()])]);
- }
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('The AI drew :count new logos. Pick one below.', ['count' => $made])]);
-
- return back();
- }
-
- public function destroy(Request $request, string $slug, StoreLogos $logos): RedirectResponse
- {
- $logos->remove($this->ownedStore($request, $slug));
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Logo removed. Your store shows its name instead.')]);
-
- return back();
- }
-
- private function ownedStore(Request $request, string $slug): Store
- {
- return $request->user()->stores()->where('slug', $slug)->firstOrFail();
- }
- }
|