X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FTechnique.php;h=cc94327389a5d3476939c9a830398ee6fb5c3166;hb=212561cf1c6724b52c490104f5a2b4c3418b1c62;hp=2caa5f320650d296d31d9bbdb0bf6ffbe8e9cbf5;hpb=4ac9a5a331949fcec42378ddc495385ae8628a79;p=alttp.git diff --git a/app/Models/Technique.php b/app/Models/Technique.php index 2caa5f3..cc94327 100644 --- a/app/Models/Technique.php +++ b/app/Models/Technique.php @@ -4,12 +4,55 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\App; class Technique extends Model { use HasFactory; + public static function locale2lang($locale) { + if (strtolower(substr($locale, 0, 2)) == 'de') return 'de'; + return 'en'; + } + + public function chapters() { + return $this + ->belongsToMany(Technique::class, 'technique_chapter', 'parent_id', 'child_id') + ->withPivot('level', 'order') + ->orderByPivot('order') + ->using(TechniqueChapter::class); + } + + public function relations() { + return $this + ->belongsToMany(Technique::class, 'technique_relations', 'from_id', 'to_id') + ->withPivot('type') + ->using(TechniqueRelation::class); + } + + public function translations() { + return $this->hasMany(TechniqueTranslation::class); + } + + public function getTranslatedProperty($prop, $lang = null) { + if (is_null($lang)) $lang = App::getLocale(); + foreach ($this->translations as $translation) { + if ($translation->locale == $lang) { + return $translation->{$prop}; + } + } + return $this->{$prop}; + } + protected $casts = [ 'index' => 'boolean', + 'requirements' => 'array', + 'rulesets' => 'array', + 'title_icons' => 'array', + ]; + + protected $with = [ + 'translations', ]; + }