1 | <?php |
||
2 | namespace Prateekkarki\Laragen\Models; |
||
3 | use Prateekkarki\Laragen\Models\TypeResolver; |
||
4 | use Illuminate\Support\Str; |
||
5 | |||
6 | class Module |
||
7 | { |
||
8 | /** |
||
9 | * The module name in plural snake case |
||
10 | * |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $moduleName; |
||
14 | |||
15 | /** |
||
16 | * Option to generate seo fields like 'seo_keywords', 'seo_descriptions', etc. |
||
17 | * |
||
18 | * @var boolean |
||
19 | */ |
||
20 | protected $seoFields; |
||
21 | |||
22 | /** |
||
23 | * Option to generate generic fields like 'status' and 'sort' |
||
24 | * |
||
25 | * @var boolean |
||
26 | */ |
||
27 | protected $genericFields; |
||
28 | |||
29 | /** |
||
30 | * Array of data to be seeded, this data is generated in LaragenSeeder, |
||
31 | * Data is seeded when running laragen:seed |
||
32 | * |
||
33 | * @var array|null |
||
34 | */ |
||
35 | protected $seedableData; |
||
36 | |||
37 | /** |
||
38 | * Array of multiple columns inside current module. |
||
39 | * Contains data for child modules of current module |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $multipleData; |
||
44 | |||
45 | /** |
||
46 | * Array of all the columns in the module. |
||
47 | * Contains array of different instances of \Prateekkarki\Laragen\Models\Types\LaragenType |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $columnsData; |
||
52 | |||
53 | /** |
||
54 | * Array of columns of module that are considered 'display columns', |
||
55 | * first item of display column is used in Frontend as title, |
||
56 | * other items are (to be) displayed in listing page of backend |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $displayColumns; |
||
61 | |||
62 | public function __construct($moduleName, $moduleData) |
||
63 | { |
||
64 | $this->moduleName = $moduleName; |
||
65 | $this->moduleData = $moduleData; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
66 | |||
67 | $this->setAdditionalFields(); |
||
68 | $this->setSeedableData(); |
||
69 | $this->setColumnsData(); |
||
70 | $this->setDisplayColumns(); |
||
71 | } |
||
72 | |||
73 | private function setAdditionalFields() |
||
74 | { |
||
75 | if(isset($this->moduleData['additional_fields'])){ |
||
76 | $this->seoFields = $this->moduleData['additional_fields']['seo'] ?? config('laragen.options.seo_fields'); |
||
77 | $this->genericFields = $this->moduleData['additional_fields']['generic'] ?? config('laragen.options.generic_fields'); |
||
78 | unset($this->moduleData['additional_fields']); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | private function setSeedableData() |
||
83 | { |
||
84 | if(isset($this->moduleData['data'])){ |
||
85 | $this->seedableData = $this->moduleData['data'] ?? null; |
||
86 | unset($this->moduleData['data']); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | private function setColumnsData() |
||
91 | { |
||
92 | $this->columnsData = []; |
||
93 | |||
94 | $moduleStructure = $this->moduleData['structure'] ?? (!empty($this->moduleData) ? $this->moduleData : ['title' => 'string|max:128']); |
||
95 | |||
96 | $this->multipleData = array_filter($moduleStructure, function($elem) { |
||
97 | return (is_array($elem)) ? true : false; |
||
98 | }); |
||
99 | |||
100 | if ($this->genericFields) { |
||
101 | $moduleStructure['sort'] = 'integer'; |
||
102 | $moduleStructure['status'] = 'boolean'; |
||
103 | } |
||
104 | |||
105 | if ($this->seoFields) { |
||
106 | $moduleStructure['seo_title'] = 'string|max:192'; |
||
107 | $moduleStructure['seo_keywords'] = 'string|max:256'; |
||
108 | $moduleStructure['seo_description'] = 'string|max:500'; |
||
109 | } |
||
110 | foreach ($moduleStructure as $columnName => $columnOptions) { |
||
111 | $column = TypeResolver::getType($this->moduleName, $columnName, $columnOptions); |
||
112 | $this->columnsData[$columnName] = $column; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | private function setDisplayColumns() |
||
117 | { |
||
118 | $this->displayColumns = []; |
||
119 | |||
120 | foreach ($this->columnsData as $column) { |
||
121 | if ($column->isDisplay()) |
||
122 | $this->displayColumns[] = $column; |
||
123 | } |
||
124 | |||
125 | if (sizeof($this->displayColumns) == 0) { |
||
126 | $this->displayColumns[] = array_values($this->columnsData)[0]; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public function getTabTitles() |
||
131 | { |
||
132 | $tabs = ['General']; |
||
133 | if (sizeof($this->getFilteredColumns(['isParent', 'hasPivot']))) { |
||
134 | $tabs[] = 'Relations'; |
||
135 | } |
||
136 | if (sizeof($this->getFilteredColumns('hasFile'))) { |
||
137 | $tabs[] = 'Attachments'; |
||
138 | } |
||
139 | if (sizeof($this->getFilteredColumns('hasImage'))) { |
||
140 | $tabs[] = 'Images'; |
||
141 | } |
||
142 | if (sizeof($this->getFilteredColumns('isMultipleType'))) { |
||
143 | foreach ($this->getFilteredColumns('isMultipleType') as $column) { |
||
144 | $tabs[] = Str::plural($column->getChildModel()); |
||
145 | } |
||
146 | } |
||
147 | $tabs[] = 'Seo'; |
||
148 | return $tabs; |
||
149 | } |
||
150 | |||
151 | public function getTabs() |
||
152 | { |
||
153 | $tabs = [['general', 'hasOptions']]; |
||
154 | if (sizeof($this->getFilteredColumns(['isParent', 'hasPivot']))) { |
||
155 | $tabs[] = ['isParent', 'hasPivot']; |
||
156 | } |
||
157 | if (sizeof($this->getFilteredColumns('hasFile'))) { |
||
158 | $tabs[] = 'hasFile'; |
||
159 | } |
||
160 | if (sizeof($this->getFilteredColumns('hasImage'))) { |
||
161 | $tabs[] = 'hasImage'; |
||
162 | } |
||
163 | if (sizeof($this->getFilteredColumns('isMultipleType'))) { |
||
164 | foreach ($this->getFilteredColumns('isMultipleType') as $column) { |
||
165 | $tabs[] = Str::plural($column->getChildModel()); |
||
166 | } |
||
167 | } |
||
168 | $tabs[] = 'Seo'; |
||
169 | return $tabs; |
||
170 | } |
||
171 | |||
172 | public function getColumnsData() |
||
173 | { |
||
174 | return $this->columnsData; |
||
175 | } |
||
176 | |||
177 | public function getDisplayColumns() |
||
178 | { |
||
179 | return $this->displayColumns; |
||
180 | } |
||
181 | |||
182 | public function getPivotalColumns() |
||
183 | { |
||
184 | $relativeTypes = []; |
||
185 | foreach ($this->columnsData as $column) { |
||
186 | if ($column->isRelational() && $column->hasPivot()) { |
||
187 | $relativeTypes[] = $column; |
||
188 | } |
||
189 | } |
||
190 | return $relativeTypes; |
||
191 | } |
||
192 | |||
193 | public function getFilteredColumns($options = [], $columnsOnly = false) |
||
194 | { |
||
195 | $filteredTypes = []; |
||
196 | $options = is_array($options) ? $options : [$options]; |
||
197 | foreach ($this->columnsData as $column) { |
||
198 | foreach ($options as $option) { |
||
199 | if ($column->$option()) { |
||
200 | $filteredTypes[] = $columnsOnly ? $column->getColumn() : $column; |
||
201 | break; |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | return $filteredTypes; |
||
206 | } |
||
207 | |||
208 | public function getColumns($onlyNonRelational = false, $columnsOnly = false) |
||
209 | { |
||
210 | $columns = []; |
||
211 | foreach ($this->columnsData as $column) { |
||
212 | if ($onlyNonRelational && $column->isRelational()) { |
||
213 | continue; |
||
214 | } |
||
215 | if ($columnsOnly) { |
||
216 | $columns[] = $column->getColumnKey(); |
||
217 | } else { |
||
218 | $columns[$column->getColumnKey()] = $column; |
||
219 | } |
||
220 | } |
||
221 | return $columns; |
||
222 | } |
||
223 | |||
224 | public function getLastColumn() |
||
225 | { |
||
226 | $keyArray = array_keys($this->getColumns(true, true)); |
||
227 | $lastColumn = array_pop($keyArray); |
||
228 | return $lastColumn; |
||
229 | } |
||
230 | |||
231 | public function getModuleName() |
||
232 | { |
||
233 | return $this->moduleName; |
||
234 | } |
||
235 | |||
236 | public function getModuleDisplayName() |
||
237 | { |
||
238 | return Str::title(str_replace('_', '', $this->moduleName)); |
||
239 | } |
||
240 | |||
241 | public function getModelName() |
||
242 | { |
||
243 | return ucfirst(Str::camel(Str::singular($this->moduleName))); |
||
244 | } |
||
245 | |||
246 | public function getModelNamePlural() |
||
247 | { |
||
248 | return ucfirst(Str::camel($this->moduleName)); |
||
249 | } |
||
250 | |||
251 | public function getModelNameLowercase() |
||
252 | { |
||
253 | return Str::singular($this->moduleName); |
||
254 | } |
||
255 | } |
||
256 |