|
1
|
|
|
<?php |
|
2
|
|
|
namespace Prateekkarki\Laragen\Models; |
|
3
|
|
|
use Prateekkarki\Laragen\Models\TypeResolver; |
|
4
|
|
|
use Illuminate\Support\Str; |
|
5
|
|
|
|
|
6
|
|
|
class Module |
|
7
|
|
|
{ |
|
8
|
|
|
protected $name; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
public function __construct($moduleName, $moduleData) |
|
12
|
|
|
{ |
|
13
|
|
|
$this->name = $moduleName; |
|
14
|
|
|
|
|
15
|
|
|
$this->seoFields = $moduleData['additional_fields']['seo'] ?? config('laragen.options.seo_fields'); |
|
|
|
|
|
|
16
|
|
|
$this->genericFields = $moduleData['additional_fields']['generic'] ?? config('laragen.options.generic_fields'); |
|
|
|
|
|
|
17
|
|
|
unset($moduleData['additional_fields']); |
|
18
|
|
|
|
|
19
|
|
|
$this->seedableData = $moduleData['data'] ?? false; |
|
|
|
|
|
|
20
|
|
|
unset($moduleData['data']); |
|
21
|
|
|
|
|
22
|
|
|
$moduleStructure = $moduleData['structure'] ?? (!empty($moduleData) ? $moduleData : ['title' => 'string|max:128']); |
|
23
|
|
|
unset($moduleData['structure']); |
|
24
|
|
|
|
|
25
|
|
|
// $this->multipleData = []; |
|
26
|
|
|
$this->multipleData = array_filter($moduleStructure, function($elem) { |
|
|
|
|
|
|
27
|
|
|
return (is_array($elem)) ? true : false; |
|
28
|
|
|
}); |
|
29
|
|
|
|
|
30
|
|
|
if ($this->genericFields) { |
|
31
|
|
|
$moduleStructure['sort'] = 'integer'; |
|
32
|
|
|
$moduleStructure['status'] = 'boolean'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if ($this->seoFields) { |
|
36
|
|
|
$moduleStructure['seo_title'] = 'string|max:192'; |
|
37
|
|
|
$moduleStructure['seo_keywords'] = 'string|max:256'; |
|
38
|
|
|
$moduleStructure['seo_description'] = 'string|max:500'; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->columnsData = []; |
|
|
|
|
|
|
42
|
|
|
$this->seedData = $moduleData['data'] ?? null; |
|
|
|
|
|
|
43
|
|
|
$this->displayColumns = []; |
|
|
|
|
|
|
44
|
|
|
foreach ($moduleStructure as $column => $typeOptions) { |
|
45
|
|
|
$data = new TypeResolver($this->name, $column, $typeOptions); |
|
46
|
|
|
$type = $data->getLaragenType(); |
|
47
|
|
|
$this->columnsData[$column] = $type; |
|
48
|
|
|
if($type->isDisplay()) |
|
49
|
|
|
$this->displayColumns[] = $type; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if(sizeof($this->displayColumns)==0){ |
|
53
|
|
|
$this->displayColumns[] = array_values($this->columnsData)[0]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getTabTitles() |
|
60
|
|
|
{ |
|
61
|
|
|
$tabs = ['General']; |
|
62
|
|
|
if (sizeof($this->getFilteredColumns(['isParent', 'hasPivot']))) { |
|
63
|
|
|
$tabs[] = 'Relations'; |
|
64
|
|
|
} |
|
65
|
|
|
if (sizeof($this->getFilteredColumns('hasFile'))) { |
|
66
|
|
|
$tabs[] = 'Attachments'; |
|
67
|
|
|
} |
|
68
|
|
|
if (sizeof($this->getFilteredColumns('hasImage'))) { |
|
69
|
|
|
$tabs[] = 'Images'; |
|
70
|
|
|
} |
|
71
|
|
|
if (sizeof($this->getFilteredColumns('isMultipleType'))) { |
|
72
|
|
|
foreach ($this->getFilteredColumns('isMultipleType') as $type) { |
|
73
|
|
|
$tabs[] = Str::plural($type->getChildModel()); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
$tabs[] = 'Seo'; |
|
77
|
|
|
return $tabs; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getTabs() |
|
81
|
|
|
{ |
|
82
|
|
|
$tabs = [['general', 'hasOptions']]; |
|
83
|
|
|
if (sizeof($this->getFilteredColumns(['isParent', 'hasPivot']))) { |
|
84
|
|
|
$tabs[] = ['isParent', 'hasPivot']; |
|
85
|
|
|
} |
|
86
|
|
|
if (sizeof($this->getFilteredColumns('hasFile'))) { |
|
87
|
|
|
$tabs[] = 'hasFile'; |
|
88
|
|
|
} |
|
89
|
|
|
if (sizeof($this->getFilteredColumns('hasImage'))) { |
|
90
|
|
|
$tabs[] = 'hasImage'; |
|
91
|
|
|
} |
|
92
|
|
|
if (sizeof($this->getFilteredColumns('isMultipleType'))) { |
|
93
|
|
|
foreach ($this->getFilteredColumns('isMultipleType') as $type) { |
|
94
|
|
|
$tabs[] = Str::plural($type->getChildModel()); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
$tabs[] = 'Seo'; |
|
98
|
|
|
return $tabs; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getColumnsData() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->columnsData; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function getDisplayColumns() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->displayColumns; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function getPivotalColumns() |
|
112
|
|
|
{ |
|
113
|
|
|
$relativeTypes = []; |
|
114
|
|
|
foreach($this->columnsData as $type){ |
|
115
|
|
|
if($type->isRelational()&&$type->hasPivot()){ |
|
116
|
|
|
$relativeTypes[] = $type; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
return $relativeTypes; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getFilteredColumns($options = [], $columnsOnly = false) |
|
123
|
|
|
{ |
|
124
|
|
|
$filteredTypes = []; |
|
125
|
|
|
$options = is_array($options) ? $options : [$options]; |
|
126
|
|
|
foreach($this->columnsData as $type){ |
|
127
|
|
|
foreach ($options as $option) { |
|
128
|
|
|
if($type->$option()){ |
|
129
|
|
|
$filteredTypes[] = $columnsOnly ? $type->getColumn() : $type; |
|
130
|
|
|
break; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
return $filteredTypes; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function getColumns($onlyNonRelational = false, $columnsOnly = false) |
|
138
|
|
|
{ |
|
139
|
|
|
$columns = []; |
|
140
|
|
|
foreach($this->columnsData as $type){ |
|
141
|
|
|
if($onlyNonRelational && $type->isRelational()){ |
|
142
|
|
|
continue; |
|
143
|
|
|
} |
|
144
|
|
|
if($columnsOnly){ |
|
145
|
|
|
$columns[] = $type->getColumnKey(); |
|
146
|
|
|
}else{ |
|
147
|
|
|
$columns[$type->getColumnKey()] = $type; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
return $columns; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function getName() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->name; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function hasPivotRelations() |
|
159
|
|
|
{ |
|
160
|
|
|
$hasRelations = false; |
|
161
|
|
|
foreach($this->columnsData as $column => $type){ |
|
162
|
|
|
if($type->isRelational()&&$type->hasPivot()){ |
|
163
|
|
|
$hasRelations = true; |
|
164
|
|
|
break; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
return $hasRelations; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function getMultipleColumns() |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->multipleData; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function getLastColumn() |
|
176
|
|
|
{ |
|
177
|
|
|
$keyArray = array_keys($this->getColumns(true, true)); |
|
178
|
|
|
$lastColumn = array_pop($keyArray); |
|
179
|
|
|
return $lastColumn; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function getModuleName() |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->name; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function getModuleDisplayName() |
|
188
|
|
|
{ |
|
189
|
|
|
return Str::title(str_replace('_', '', $this->name)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function getModelName() |
|
193
|
|
|
{ |
|
194
|
|
|
return ucfirst(Str::camel(str_singular($this->name))); |
|
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function getModelNamePlural() |
|
198
|
|
|
{ |
|
199
|
|
|
return ucfirst(Str::camel($this->name)); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function getModelNameLowercase() |
|
203
|
|
|
{ |
|
204
|
|
|
return str_singular($this->name); |
|
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|