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; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getRelationNameAttribute() |
75
|
|
|
{ |
76
|
|
|
$relationName = null; |
77
|
|
|
|
78
|
|
|
if ($this->type === 'n-n') { |
|
|
|
|
79
|
|
|
$relationName = $this->data->relationName ?? Str::plural($this->relatedModule->name); |
|
|
|
|
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') { |
|
|
|
|
102
|
|
|
$params[ $this->relatedField->name ] = $sourceRecord->getKey(); |
|
|
|
|
103
|
|
|
$params[ $this->relatedField->name.'_display' ] = $sourceRecord->recordLabel ?? $sourceRecord->getKey(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Add tab id if defined |
107
|
|
|
if ($this->tab_id) { |
|
|
|
|
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) { |
|
|
|
|
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 |
|
|
|
|
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) { |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.