| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\StorePlan\PlanSchema;
- use App\StorePlan\PlanValidator;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- use Inertia\Inertia;
-
- /**
- * The Design tab's Style box: a ready-made look (style, corner roundness and fonts), or single style options.
- * Colours are never touched. This changes the working design, so a live store shows it after "Publish changes".
- */
- class StoreStyleController extends Controller
- {
- public function update(Request $request, PlanValidator $validator, string $slug): RedirectResponse
- {
- $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
-
- abort_if($store->plan === null, 404);
-
- $rules = ['preset' => ['nullable', Rule::in(array_keys(PlanSchema::STYLE_PRESETS))], 'style' => ['nullable', 'array']];
-
- foreach (PlanSchema::STYLE_OPTIONS as $option => $values) {
- $rules["style.$option"] = ['nullable', Rule::in($values)];
- }
-
- $validated = $request->validate($rules);
- $plan = $store->plan;
- $preset = PlanSchema::STYLE_PRESETS[$validated['preset'] ?? ''] ?? null;
-
- if ($preset !== null) {
- $plan['theme']['radius'] = $preset['radius'];
- $plan['theme']['fonts'] = $preset['fonts'];
- $plan['theme']['style'] = $preset['style'];
- }
-
- if (is_array($validated['style'] ?? null)) {
- $plan['theme']['style'] = [...PlanSchema::STYLE_DEFAULTS, ...($plan['theme']['style'] ?? []), ...array_filter($validated['style'])];
- }
-
- $store->plan = $validator->validate($plan);
- $store->save();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => $store->isPublished()
- ? __('Saved. Click "Publish changes" to show it on your live store.')
- : __('Style saved.')]);
-
- return to_route('stores.show', $store->slug);
- }
- }
|