comparison src/Generator/View/IndexViewGenerator.php @ 32:45f384a24553 codex

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