user(); // Pre-check only when user has already accepted the current version if ($user && $user->hasAcceptedPrinciples() && !$user->needsToReacceptPrinciples()) { $this->agreed = true; } } public function accept() { $user = Auth::guard('web')->user(); if (!$user) { $this->notification()->error( $title = __('Error'), $description = __('You must be logged in to accept the principles.') ); return; } if (!$this->agreed) { $this->notification()->warning( $title = __('Confirmation required'), $description = __('Please check the box to confirm you accept the principles.') ); return; } // Get the current principles post with active translation $principlesPost = $this->getCurrentPrinciplesPost(); if (!$principlesPost || !$principlesPost->translations->first()) { $this->notification()->error( $title = __('Error'), $description = __('Unable to find the current principles document.') ); return; } $translation = $principlesPost->translations->first(); // Save acceptance with version tracking $user->update([ 'principles_terms_accepted' => [ 'post_id' => $principlesPost->id, 'post_translation_id' => $translation->id, 'locale' => $translation->locale, 'from' => $translation->from, 'updated_at' => $translation->updated_at->toDateTimeString(), 'accepted_at' => now()->toDateTimeString(), ] ]); $this->notification()->success( $title = __('Thank you'), $description = __('Your acceptance has been recorded.') ); // Sent here on the way to another page: continue there $intended = session()->pull('principles_intended_url'); if ($intended) { return $this->redirect($intended); } // Refresh the component to show the acceptance status $this->dispatch('$refresh'); } protected function getCurrentPrinciplesPost() { $locale = app()->getLocale(); $fallback = config('app.fallback_locale', 'en'); $activeTranslationQuery = function ($query) { $query->where('from', '<=', now()) ->where(function ($q) { $q->where('till', '>', now())->orWhereNull('till'); }); }; $post = Post::whereHas('category', function ($query) { $query->where('type', 'SiteContents\Static\Principles'); }) ->whereHas('translations', $activeTranslationQuery) ->first(); if (!$post) { return null; } // Load the best available translation: user locale → fallback locale → any active $translation = $post->translations() ->where(function ($q) use ($locale) { $q->where('locale', $locale)->orWhere('locale', 'like', $locale . '%'); }) ->where('from', '<=', now()) ->where(function ($q) { $q->where('till', '>', now())->orWhereNull('till'); }) ->first(); if (!$translation) { $translation = $post->translations() ->where(function ($q) use ($fallback) { $q->where('locale', $fallback)->orWhere('locale', 'like', $fallback . '%'); }) ->where('from', '<=', now()) ->where(function ($q) { $q->where('till', '>', now())->orWhereNull('till'); }) ->first(); } if (!$translation) { $translation = $post->translations() ->where('from', '<=', now()) ->where(function ($q) { $q->where('till', '>', now())->orWhereNull('till'); }) ->first(); } $post->setRelation('translations', $translation ? collect([$translation]) : collect()); return $post; } public function render() { $user = Auth::guard('web')->user(); $hasAccepted = $user && $user->hasAcceptedPrinciples(); $needsReaccept = $user && $user->needsToReacceptPrinciples(); $acceptedData = $user?->principles_terms_accepted; return view('livewire.accept-principles', [ 'user' => $user, 'hasAccepted' => $hasAccepted, 'needsReaccept' => $needsReaccept, 'acceptedData' => $acceptedData, ]); } }