HEX
Server: LiteSpeed
System: Linux us-phx-web629.main-hosting.eu 5.14.0-503.23.2.el9_5.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Feb 12 05:52:18 EST 2025 x86_64
User: u756937133 (756937133)
PHP: 8.2.27
Disabled: passthru,chgrp
Upload Files
File: /home/u756937133/domains/swingersnest.com/public_html/app/Traits/Translatable.php
<?php

namespace App\Traits;

use App\Models\Translation;
use Illuminate\Support\Facades\App;

trait Translatable
{
    /**
     * Get translations relationship
     */
    public function translations()
    {
        return $this->morphMany(Translation::class, 'translatable');
    }

    /**
     * Get a translated attribute value
     */
    public function getTranslatedAttribute(string $field, ?string $locale = null): ?string
    {
        $locale = $locale ?? App::getLocale();
        
        // If locale is 'en' (default), return original value from attributes
        if ($locale === 'en') {
            return $this->attributes[$field] ?? null;
        }
        
        // Get translation from database
        $translation = $this->translations()
            ->where('locale', $locale)
            ->where('field', $field)
            ->first();
        
        // Return translation if exists and has value
        if ($translation && !empty($translation->value)) {
            return $translation->value;
        }
        
        // Fallback to English value from attributes
        return $this->attributes[$field] ?? null;
    }

    /**
     * Set a translation for a specific field and locale
     */
    public function setTranslation(string $field, string $locale, ?string $value): void
    {
        if ($locale === 'en') {
            // For English, update the main model attribute
            $this->attributes[$field] = $value;
            $this->save();
            return;
        }
        
        // For other languages, store in translations table
        $this->translations()->updateOrCreate(
            [
                'locale' => $locale,
                'field' => $field,
            ],
            [
                'value' => $value,
            ]
        );
    }

    /**
     * Set multiple translations at once
     */
    public function setTranslations(array $translations): void
    {
        foreach ($translations as $locale => $fields) {
            foreach ($fields as $field => $value) {
                $this->setTranslation($field, $locale, $value);
            }
        }
    }

    /**
     * Get all translations for a specific locale
     */
    public function getTranslationsForLocale(string $locale): array
    {
        $translations = [];
        
        // Get translatable fields from model
        $translatableFields = $this->getTranslatableFields();
        
        foreach ($translatableFields as $field) {
            $translations[$field] = $this->getTranslatedAttribute($field, $locale);
        }
        
        return $translations;
    }

    /**
     * Get translatable fields - override in model if needed
     */
    protected function getTranslatableFields(): array
    {
        return $this->translatableFields ?? [];
    }

    /**
     * Magic method to get translated attributes
     * Only intercepts translatable fields when not in English locale
     */
    public function __get($key)
    {
        $translatableFields = $this->getTranslatableFields();
        
        // Only intercept translatable fields when not in English
        if (!empty($translatableFields) && in_array($key, $translatableFields) && App::getLocale() !== 'en') {
            $translated = $this->getTranslatedAttribute($key);
            // Only return translated value if it exists, otherwise fall through to parent
            if ($translated !== null && $translated !== '') {
                return $translated;
            }
        }
        
        // Default Laravel behavior for non-translatable fields or English locale
        return parent::__get($key);
    }

    /**
     * Check if translation exists for a field and locale
     */
    public function hasTranslation(string $field, string $locale): bool
    {
        if ($locale === 'en') {
            return isset($this->attributes[$field]) && !empty($this->attributes[$field]);
        }
        
        return $this->translations()
            ->where('locale', $locale)
            ->where('field', $field)
            ->whereNotNull('value')
            ->exists();
    }

    /**
     * Delete all translations for this model
     */
    public function deleteTranslations(): void
    {
        $this->translations()->delete();
    }

    /**
     * Delete translation for a specific locale
     */
    public function deleteTranslationsForLocale(string $locale): void
    {
        $this->translations()->where('locale', $locale)->delete();
    }
}