Completed
Push — master ( 97eddd...5cf35a )
by Jonathan
19:50 queued 09:35
created

Relatedlist::getIsVisibleAsTabAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Uccello\Core\Models;
4
5
use Uccello\Core\Database\Eloquent\Model;
6
7
class Relatedlist extends Model
8
{
9
    /**
10
     * The table associated with the model.
11
     *
12
     * @var string
13
     */
14
    protected $table = 'relatedlists';
15
16
    /**
17
     * The attributes that should be casted to native types.
18
     *
19
     * @var array
20
     */
21
    protected $casts = [
22
        'data' => 'object',
23
    ];
24
25
    /**
26
     * The attributes that are mass assignable.
27
     *
28
     * @var array
29
     */
30
    protected $fillable = [
31
        'module_id',
32
        'related_module_id',
33
        'tab_id',
34
        'related_field_id',
35
        'label',
36
        'icon',
37
        'type',
38
        'method',
39
        'sequence',
40
        'data',
41
    ];
42
43
    protected function initTablePrefix()
44
    {
45
        $this->tablePrefix = env('UCCELLO_TABLE_PREFIX', 'uccello_');
46
    }
47
48
    public function module()
49
    {
50
        return $this->belongsTo(Module::class);
51
    }
52
53
    public function relatedModule()
54
    {
55
        return $this->belongsTo(Module::class);
56
    }
57
58
    public function relatedTab()
59
    {
60
        return $this->belongsTo(Tab::class);
61
    }
62
63
    public function relatedField()
64
    {
65
        return $this->belongsTo(Field::class);
66
    }
67
68
    public function getIsVisibleAsTabAttribute()
69
    {
70
        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...
71
    }
72
73
    /**
74
     * Returns add link according to related list type
75
     *
76
     * @param Domain $domain
77
     * @param integer $sourceRecordId
78
     * @return string
79
     */
80
    public function getAddLink(Domain $domain, int $sourceRecordId) : string
81
    {
82
        // Default parameters
83
        $params = [
84
            'relatedlist' => $this->id,
85
            'src_id' => $sourceRecordId
86
        ];
87
88
        // If it is a N-1 related list, add value of the linked field
89
        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...
90
            $params[ $this->relatedField->name ] = $sourceRecordId;
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...
91
        }
92
93
        // Add tab id if defined
94
        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...
95
            $params[ 'tab' ] = $this->tab_id;
96
        }
97
98
        return ucroute('uccello.edit', $domain, $this->relatedModule, $params);
99
    }
100
101
    /**
102
     * Returns edit link according to related list type
103
     *
104
     * @param Domain $domain
105
     * @param integer $sourceRecordId
106
     * @return string
107
     */
108
    public function getEditLink(Domain $domain, int $sourceRecordId) : string
109
    {
110
        // Default parameters
111
        $params = [
112
            'id' => 'RECORD_ID', // RECORD_ID will be replaced automaticaly by the record id in the datatable
113
            'relatedlist' => $this->id,
114
            'src_id' => $sourceRecordId
115
        ];
116
117
        // Add tab id if defined
118
        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...
119
            $params[ 'tab' ] = $this->tab_id;
120
        }
121
122
        return ucroute('uccello.edit', $domain, $this->relatedModule, $params);
123
    }
124
125
    /**
126
     * Returns delete link according to related list type
127
     *
128
     * @param Domain $domain
129
     * @param integer $sourceRecordId
130
     * @param boolean $deleteRelation
131
     * @return string
132
     */
133
    public function getDeleteLink(Domain $domain, int $sourceRecordId, bool $preferDeleteRelation = true) : string
134
    {
135
        // Default parameters
136
        $params = [
137
            'id' => 'RECORD_ID', // RECORD_ID will be replaced automaticaly by the record id in the datatable
138
            'relatedlist' => $this->id,
139
            'src_id' => $sourceRecordId
140
        ];
141
142
        // Add tab id if defined
143
        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...
144
            $params[ 'tab' ] = $this->tab_id;
145
        }
146
147
        // If it is a N-N related list and if necessary add the relation id. It will delete the relation instead of the record
148
        if ($this->type === 'n-n' && $preferDeleteRelation === true) {
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...
149
            $params[ 'relation_id' ] = 'RELATION_ID'; // RELATION_ID will be replaced automaticaly by the relation id in the datatable
150
        }
151
152
        return ucroute('uccello.delete', $domain, $this->relatedModule, $params);
153
    }
154
155
    /**
156
     * Checks if it is possible to add a record.
157
     *
158
     * @return boolean
159
     */
160
    public function canAdd() : bool
161
    {
162
        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...
163
    }
164
165
    /**
166
     * Checks if it is possible to select a record.
167
     *
168
     * @return boolean
169
     */
170
    public function canSelect() : bool
171
    {
172
        return $this->type === 'n-n' && isset($this->data->actions) && in_array('select', $this->data->actions);
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...
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...
173
    }
174
}
175