FormField::translations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace MedianetDev\BackpackForm\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Backpack\CRUD\app\Models\Traits\CrudTrait;
7
use Illuminate\Support\Facades\App;
8
9
class FormField extends Model
10
{
11
    use CrudTrait;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Models\Traits\CrudTrait requires some properties which are not provided by MedianetDev\BackpackForm\Models\FormField: $fakeColumns, $identifiableAttribute, $Type
Loading history...
12
13
    /*
14
    |--------------------------------------------------------------------------
15
    | GLOBAL VARIABLES
16
    |--------------------------------------------------------------------------
17
    */
18
    protected $table = 'med_form_fields';
19
    protected $guarded = ['id'];
20
21
    /*
22
    |--------------------------------------------------------------------------
23
    | FUNCTIONS
24
    |--------------------------------------------------------------------------
25
    */
26
    protected $fillable = [
27
        'key',
28
        'form_id',
29
        'form_step_id',
30
        'type',
31
        'is_required',
32
        'is_active',
33
        'order',
34
        'formio_component',
35
        'validation_rules',
36
        'order',
37
    ];
38
39
40
41
    public function translation()
42
    {
43
        return $this->hasOne(FormFieldTranslation::class)->where('locale', locale());
0 ignored issues
show
Bug introduced by
The function locale was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        return $this->hasOne(FormFieldTranslation::class)->where('locale', /** @scrutinizer ignore-call */ locale());
Loading history...
44
    }
45
46
    public function translations()
47
    {
48
        return $this->hasMany(FormFieldTranslation::class);
49
    }
50
    public function conditions()
51
    {
52
        return $this->hasMany(FormFieldCondition::class, 'form_field_id');
53
    }
54
55
    /**
56
     * Get translated attribute
57
     *
58
     * @param string $attribute
59
     * @param string|null $locale
60
     * @return mixed
61
     */
62
    public function getTranslatedAttribute($attribute, $locale = null)
63
    {
64
        $locale = $locale ?: locale();
0 ignored issues
show
Bug introduced by
The function locale was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $locale = $locale ?: /** @scrutinizer ignore-call */ locale();
Loading history...
65
66
        if ($translation = $this->translations->where('locale', $locale)->first()) {
67
            return $translation->{$attribute};
68
        }
69
70
        return null;
71
    }
72
73
74
75
76
77
78
79
    /*
80
    |--------------------------------------------------------------------------
81
    | RELATIONS
82
    |--------------------------------------------------------------------------
83
    */
84
85
    /**
86
     * Get the form that owns the field
87
     */
88
    public function form()
89
    {
90
        return $this->belongsTo(Formbuilder::class, 'form_id');
91
    }
92
93
    /**
94
     * Get the form step that owns the field
95
     */
96
    public function formStep()
97
    {
98
        return $this->belongsTo('MedianetDev\BackpackForm\Models\FormStep', 'form_step_id');
99
    }
100
101
    /*
102
    |--------------------------------------------------------------------------
103
    | SCOPES
104
    |--------------------------------------------------------------------------
105
    */
106
107
    /**
108
     * Only active fields
109
     */
110
    public function scopeActive($query)
111
    {
112
        return $query->where('is_active', 1);
113
    }
114
115
    /**
116
     * Only required fields
117
     */
118
    public function scopeRequired($query)
119
    {
120
        return $query->where('is_required', 1);
121
    }
122
123
    /*
124
    |--------------------------------------------------------------------------
125
    | ACCESSORS
126
    |--------------------------------------------------------------------------
127
    */
128
129
    /*
130
    |--------------------------------------------------------------------------
131
    | MUTATORS
132
    |--------------------------------------------------------------------------
133
    */
134
}
135