]> git.localhorst.tv Git - alttp.git/blob - app/Models/Technique.php
refine chat gen
[alttp.git] / app / Models / Technique.php
1 <?php
2
3 namespace App\Models;
4
5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Support\Facades\App;
8
9 class Technique extends Model
10 {
11         use HasFactory;
12
13         public static function locale2lang($locale) {
14                 if (strtolower(substr($locale, 0, 2)) == 'de') return 'de';
15                 return 'en';
16         }
17
18         public function chapters() {
19                 return $this
20                         ->belongsToMany(Technique::class, 'technique_chapter', 'parent_id', 'child_id')
21                         ->withPivot('level', 'order')
22                         ->orderByPivot('order')
23                         ->using(TechniqueChapter::class);
24         }
25
26         public function relations() {
27                 return $this
28                         ->belongsToMany(Technique::class, 'technique_relations', 'from_id', 'to_id')
29                         ->withPivot('type')
30                         ->using(TechniqueRelation::class);
31         }
32
33         public function translations() {
34                 return $this->hasMany(TechniqueTranslation::class);
35         }
36
37         public function getTranslatedProperty($prop, $lang = null) {
38                 if (is_null($lang)) $lang = App::getLocale();
39                 foreach ($this->translations as $translation) {
40                         if ($translation->locale == $lang) {
41                                 return $translation->{$prop};
42                         }
43                 }
44                 return $this->{$prop};
45         }
46
47         protected $casts = [
48                 'index' => 'boolean',
49                 'requirements' => 'array',
50                 'rulesets' => 'array',
51                 'title_icons' => 'array',
52         ];
53
54         protected $with = [
55                 'translations',
56         ];
57
58 }