Relatedlist   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 49
c 1
b 0
f 0
dl 0
loc 171
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getIsVisibleAsTabAttribute() 0 3 1
A relatedField() 0 3 1
A relatedTab() 0 3 1
A relatedModule() 0 3 1
A initTablePrefix() 0 3 1
A module() 0 3 1
A getEditLink() 0 15 2
A canAdd() 0 3 2
A canSelect() 0 3 2
A getDeleteLink() 0 15 2
A getRelationNameAttribute() 0 7 1
A getAddLink() 0 20 3
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $relationName is dead and can be removed.
Loading history...
77
78
        $relationName = $this->data->relationName ?? Str::camel(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...
79
80
        return $relationName;
81
    }
82
83
    /**
84
     * Returns add link according to related list type
85
     *
86
     * @param Domain $domain
87
     * @param $sourceRecord
88
     * @return string
89
     */
90
    public function getAddLink(Domain $domain, $sourceRecord) : string
91
    {
92
        // Default parameters
93
        $params = [
94
            'relatedlist' => $this->id,
95
            'src_id' => $sourceRecord->getKey(),
96
        ];
97
98
        // If it is a N-1 related list, add value of the linked field
99
        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...
100
            $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...
101
            $params[ $this->relatedField->name.'_display' ] = $sourceRecord->recordLabel ?? $sourceRecord->getKey();
102
        }
103
104
        // Add tab id if defined
105
        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...
106
            $params[ 'tab' ] = $this->tab_id;
107
        }
108
109
        return ucroute('uccello.edit', $domain, $this->relatedModule, $params);
110
    }
111
112
    /**
113
     * Returns edit link according to related list type
114
     *
115
     * @param Domain $domain
116
     * @param integer $sourceRecordId
117
     * @return string
118
     */
119
    public function getEditLink(Domain $domain, int $sourceRecordId) : string
120
    {
121
        // Default parameters
122
        $params = [
123
            'id' => 'RECORD_ID', // RECORD_ID will be replaced automaticaly by the record id in the datatable
124
            'relatedlist' => $this->id,
125
            'src_id' => $sourceRecordId
126
        ];
127
128
        // Add tab id if defined
129
        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...
130
            $params[ 'tab' ] = $this->tab_id;
131
        }
132
133
        return ucroute('uccello.edit', $domain, $this->relatedModule, $params);
134
    }
135
136
    /**
137
     * Returns delete link according to related list type
138
     *
139
     * @param Domain $domain
140
     * @param integer $sourceRecordId
141
     * @param boolean $deleteRelation
142
     * @return string
143
     */
144
    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

144
    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...
145
    {
146
        // Default parameters
147
        $params = [
148
            'id' => $sourceRecordId,
149
            'relatedlist' => $this->id,
150
            'related_id' => 'RECORD_ID', // RECORD_ID will be replaced automaticaly by the record id in the datatable
151
        ];
152
153
        // Add tab id if defined
154
        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...
155
            $params['tab'] = $this->tab_id;
156
        }
157
158
        return ucroute('uccello.edit.relation.delete', $domain, $this->relatedModule, $params);
159
    }
160
161
    /**
162
     * Checks if it is possible to add a record.
163
     *
164
     * @return boolean
165
     */
166
    public function canAdd() : bool
167
    {
168
        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...
169
    }
170
171
    /**
172
     * Checks if it is possible to select a record.
173
     *
174
     * @return boolean
175
     */
176
    public function canSelect() : bool
177
    {
178
        return 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...
179
    }
180
}
181