comparison src/Generator/View/IndexViewGenerator.php @ 26:555bfaa500ac codex

Big update for view creation based on the column type.
author Luka Sitas <sitas.luka.97@gmail.com>
date Thu, 15 May 2025 21:50:38 -0400
parents 1a717c7b211f
children 8dd668020310
comparison
equal deleted inserted replaced
25:1a717c7b211f 26:555bfaa500ac
1 <?php 1 <?php
2 2
3 namespace Wizard\MagicForger\Generator\View; 3 namespace Wizard\MagicForger\Generator\View;
4 4
5 use Symfony\Component\Console\Attribute\AsCommand; 5 use Symfony\Component\Console\Attribute\AsCommand;
6 use Wizard\MagicForger\Helpers\RelationshipNavigator;
6 use Wizard\MagicForger\Generator\BaseGenerator; 7 use Wizard\MagicForger\Generator\BaseGenerator;
8 use Illuminate\Support\Str;
7 9
8 #[AsCommand(name: 'mf:index_view')] 10 #[AsCommand(name: 'mf:index_view')]
9 class IndexViewGenerator extends BaseGenerator 11 class IndexViewGenerator extends BaseGenerator
10 { 12 {
11 /** 13 /**
72 */ 74 */
73 protected function getPath($name = null) 75 protected function getPath($name = null)
74 { 76 {
75 return str_replace(['Resources\\', '\\'], ['resources/', '/'], $this->getViewNamespace($this->getTableInput()).'index.blade.php'); 77 return str_replace(['Resources\\', '\\'], ['resources/', '/'], $this->getViewNamespace($this->getTableInput()).'index.blade.php');
76 } 78 }
79
80
81 protected function renderColumns() {
82 $renders = [];
83 $headers = [];
84 $values = [];
85 $colCount = 0;
86
87 $columns = $this->getTableColumns($this->getCurrentTable());
88 $relations = RelationshipNavigator::getRelations($this->getCurrentTable());
89 //gether the select columns based on the relations
90 $selects = [];
91
92 foreach($relations['belongsTo'] as $relation) {
93 $selects[$relation['column']] = $relation['table'];
94 }
95
96 foreach($columns as $column) {
97 $name = $column['name'];
98 if(in_array($name, $this->columns_to_ignore)) continue;
99
100
101 //Get the expected header name
102 $replacements = [
103 '{{header}}' => Str::headline($name),
104 '{{headerClass}}' => 'p-2',
105 '{{value}}' => '{{ $item->' . $name . ' ?? "" }}',
106 '{{valueClass}}' => 'p-2',
107 ];
108
109 $type = $column['type_name'];
110
111
112 //date
113 if(in_array($type, ['date'])) {
114 $replacements['{{value}}'] = '{{ $item->'.$name.'?->format(\'Y-m-d\') ?? "" }}';
115 }
116 //time
117 if(in_array($type, ['timestamp'])) {
118 $replacements['{{value}}'] = '{{ $item->'.$name.'?->format(\'Y-m-d H:i\') ?? "" }}';
119 }
120 //checkbox
121 elseif(in_array($type, ['tinyint'])) {
122 $replacements['{{valueClass}}'] .= ' text-center';
123 $replacements['{{value}}'] = '{{ $item->' . $name . ' ?? "0" }}';
124 }
125 //select
126 elseif(in_array($type, ['bigint']) && array_key_exists($name, $selects)) {
127 $replacements['{{header}}'] = Str::headline(Str::singular($selects[$name]));
128 $replacements['{{value}}'] = '{{ $item->'.Str::singular($selects[$name]) . '?->name ?? "" }}';
129 }
130 // bigint, float
131 elseif(in_array($type, ['bigint', 'float', 'int'])) {
132 $replacements['{{valueClass}}'] .= ' text-start';
133 }
134 else {
135 //text area
136 //varchar, , etc
137 }
138
139 $snippet = $this->getSnippet('index/value');
140 // Replace placeholders with actual values
141 $values[] = str_replace(
142 array_keys($replacements),
143 $replacements,
144 $snippet
145 );
146
147 $snippet = $this->getSnippet('index/header');
148 // Replace placeholders with actual values
149 $headers[] = str_replace(
150 array_keys($replacements),
151 $replacements,
152 $snippet
153 );
154 $colCount++;
155 }
156
157 return ['headers' => $headers, 'values' => $values, 'colCount' => $colCount];
158 }
159
160 /**
161 * Get available insertions including model relationships.
162 *
163 * @return array
164 */
165 public function get_available_inserts(): array
166 {
167 // Merge parent insertions (attributes, fillable, etc.)
168 $inserts = parent::get_available_inserts();
169
170 // Gather and render relationships for this model
171 $rendered = $this->renderColumns();
172
173 // Build code blocks for each relation type
174 $headers = '';
175 $values = '';
176 $colCount = '';
177
178 if(!empty($rendered)) {
179 $headers = !empty($rendered['headers']) ? implode("\n ", $rendered['headers']) : '';
180 $values = !empty($rendered['values']) ? implode("\n ", $rendered['values']) : '';
181 $colCount = isset($rendered['colCount']) ? $rendered['colCount'] : '';
182 }
183
184 // Assign to stub placeholders
185 $inserts['{{ headerInsertPoint }}'] = $headers;
186 $inserts['{{ valueInsertPoint }}'] = $values;
187 $inserts['{{ colCount }}'] = $colCount;
188
189 return $inserts;
190 }
77 } 191 }