Passed
Push — master ( 658575...3c4def )
by Jonathan
14:06
created

Relatedlist::getRelationNameAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Uccello\Core\Models;
4
5
use Uccello\Core\Database\Eloquent\Model;
6
use Illuminate\Support\Str;
7
8
class Relatedlist extends Model
9
{
10
    /**
11
     * The table associated with the model.
12
     *
13
     * @var string
14
     */
15
    protected $table = 'relatedlists';
16
17
    /**
18
     * The attributes that should be casted to native types.
19
     *
20
     * @var array
21
     */
22
    protected $casts = [
23
        'data' => 'object',
24
    ];
25
26
    /**
27
     * The attributes that are mass assignable.
28
     *
29
     * @var array
30
     */
31
    protected $fillable = [
32
        'module_id',
33
        'related_module_id',
34
        'tab_id',
35
        'related_field_id',
36
        'label',
37
        'icon',
38
        'type',
39
        'method',
40
        'sequence',
41
        'data',
42
    ];
43
44
    protected function initTablePrefix()
45
    {
46
        $this->tablePrefix = env('UCCELLO_TABLE_PREFIX', 'uccello_');
47
    }
48
49
    public function module()
50
    {
51
        return $this->belongsTo(Module::class);
52
    }
53
54
    public function relatedModule()
55
    {
56
        return $this->belongsTo(Module::class);
57
    }
58
59
    public function relatedTab()
60
    {
61
        return $this->belongsTo(Tab::class);
62
    }
63
64
    public function relatedField()
65
    {
66
        return $this->belongsTo(Field::class);
67
    }
68
69
    public function getIsVisibleAsTabAttribute()
70
    {
71
        return $this->data->add_tab ?? true;
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
72
    }
73
74
    public function getRelationNameAttribute()
75
    {
76
        $relationName = null;
77
78
        if ($this->type === 'n-n') {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
79
            $relationName = $this->data->relationName ?? Str::plural($this->relatedModule->name);
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property name does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
80
        }
81
82
        return $relationName;
83
    }
84
85
    /**
86
     * Returns add link according to related list type
87
     *
88
     * @param Domain $domain
89
     * @param $sourceRecord
90
     * @return string
91
     */
92
    public function getAddLink(Domain $domain, $sourceRecord) : string
93
    {
94
        // Default parameters
95
        $params = [
96
            'relatedlist' => $this->id,
97
            'src_id' => $sourceRecord->getKey(),
98
        ];
99
100
        // If it is a N-1 related list, add value of the linked field
101
        if ($this->type === 'n-1') {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
102
            $params[ $this->relatedField->name ] = $sourceRecord->getKey();
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Uccello\Core\Models\Field. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
103
            $params[ $this->relatedField->name.'_display' ] = $sourceRecord->recordLabel ?? $sourceRecord->getKey();
104
        }
105
106
        // Add tab id if defined
107
        if ($this->tab_id) {
0 ignored issues
show
Bug introduced by
The property tab_id does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
108
            $params[ 'tab' ] = $this->tab_id;
109
        }
110
111
        return ucroute('uccello.edit', $domain, $this->relatedModule, $params);
112
    }
113
114
    /**
115
     * Returns edit link according to related list type
116
     *
117
     * @param Domain $domain
118
     * @param integer $sourceRecordId
119
     * @return string
120
     */
121
    public function getEditLink(Domain $domain, int $sourceRecordId) : string
122
    {
123
        // Default parameters
124
        $params = [
125
            'id' => 'RECORD_ID', // RECORD_ID will be replaced automaticaly by the record id in the datatable
126
            'relatedlist' => $this->id,
127
            'src_id' => $sourceRecordId
128
        ];
129
130
        // Add tab id if defined
131
        if ($this->tab_id) {
0 ignored issues
show
Bug introduced by
The property tab_id does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
132
            $params[ 'tab' ] = $this->tab_id;
133
        }
134
135
        return ucroute('uccello.edit', $domain, $this->relatedModule, $params);
136
    }
137
138
    /**
139
     * Returns delete link according to related list type
140
     *
141
     * @param Domain $domain
142
     * @param integer $sourceRecordId
143
     * @param boolean $deleteRelation
144
     * @return string
145
     */
146
    public function getDeleteLink(Domain $domain, int $sourceRecordId, bool $preferDeleteRelation = true) : string
0 ignored issues
show
Unused Code introduced by
The parameter $preferDeleteRelation is not used and could be removed. ( Ignorable by Annotation )

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

146
    public function getDeleteLink(Domain $domain, int $sourceRecordId, /** @scrutinizer ignore-unused */ bool $preferDeleteRelation = true) : string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
147
    {
148
        // Default parameters
149
        $params = [
150
            'id' => 'RECORD_ID', // RECORD_ID will be replaced automaticaly by the record id in the datatable
151
            'relatedlist' => $this->id,
152
            'src_id' => $sourceRecordId
153
        ];
154
155
        // Add tab id if defined
156
        if ($this->tab_id) {
0 ignored issues
show
Bug introduced by
The property tab_id does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
157
            $params[ 'tab' ] = $this->tab_id;
158
        }
159
160
        return ucroute('uccello.delete', $domain, $this->relatedModule, $params);
161
    }
162
163
    /**
164
     * Checks if it is possible to add a record.
165
     *
166
     * @return boolean
167
     */
168
    public function canAdd() : bool
169
    {
170
        return isset($this->data->actions) && in_array('add', $this->data->actions);
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
171
    }
172
173
    /**
174
     * Checks if it is possible to select a record.
175
     *
176
     * @return boolean
177
     */
178
    public function canSelect() : bool
179
    {
180
        return $this->type === 'n-n' && isset($this->data->actions) && in_array('select', $this->data->actions);
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property type does not seem to exist on Uccello\Core\Models\Relatedlist. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
181
    }
182
}
183