comparison src/Generator/View/CreateEditViewGenerator.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
comparison
equal deleted inserted replaced
25:1a717c7b211f 26:555bfaa500ac
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\Generator\BaseGenerator; 6 use Wizard\MagicForger\Generator\BaseGenerator;
7 use Wizard\MagicForger\Helpers\RelationshipNavigator;
8 use Illuminate\Support\Str;
7 9
8 #[AsCommand(name: 'mf:create_edit_view')] 10 #[AsCommand(name: 'mf:create_edit_view')]
9 class CreateEditViewGenerator extends BaseGenerator 11 class CreateEditViewGenerator 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()).'create_edit.blade.php'); 77 return str_replace(['Resources\\', '\\'], ['resources/', '/'], $this->getViewNamespace($this->getTableInput()).'create_edit.blade.php');
76 } 78 }
79
80 protected function renderColumns() {
81 $renders = [];
82
83 $columns = $this->getTableColumns($this->getCurrentTable());
84 $relations = RelationshipNavigator::getRelations($this->getCurrentTable());
85 //gether the select columns based on the relations
86 $selects = [];
87
88 foreach($relations['belongsTo'] as $relation) {
89 $selects[$relation['column']] = $relation['table'];
90 }
91
92 foreach($columns as $column) {
93 $name = $column['name'];
94 if(in_array($name, $this->columns_to_ignore)) continue;
95
96 $type = $column['type_name'];
97
98 $replacements = [
99 '{{fieldName}}' => $name,
100 '{{fieldLabel}}' => Str::headline($name),
101 '{{required}}' => $column['nullable'] ? 'false' : 'true',
102 ];
103 $snippet = '';
104
105 //date
106 if(in_array($type, ['date', 'timestamp'])) {
107 $snippet = $this->getSnippet('input/date');
108 }
109 //checkbox
110 elseif(in_array($type, ['tinyint']) && array_key_exists($name, $selects)) {
111 $snippet = $this->getSnippet('input/checkbox');
112 }
113
114 //select
115 elseif(in_array($type, ['bigint']) && array_key_exists($name, $selects)) {
116 $snippet = $this->getSnippet('input/select');
117 $replacements['{{fieldLabel}}'] = Str::headline(Str::singular($selects[$name]));
118 $replacements['{{options}}'] = '$'.$selects[$name];
119 }
120
121 //text area
122 elseif(in_array($type, ['text'])) {
123 $snippet = $this->getSnippet('input/textarea');
124 }
125 else {
126 //varchar, bigint, float, etc
127 $snippet = $this->getSnippet('input/text');
128 }
129
130
131 // Replace placeholders with actual values
132 $renders[] = str_replace(
133 array_keys($replacements),
134 $replacements,
135 $snippet
136 );
137 }
138 return $renders;
139 }
140
141
142
143 /**
144 * Get available insertions including model relationships.
145 *
146 * @return array
147 */
148 public function get_available_inserts(): array
149 {
150 // Merge parent insertions (attributes, fillable, etc.)
151 $inserts = parent::get_available_inserts();
152
153 // Gather and render relationships for this model
154 $rendered = $this->renderColumns();
155
156 // Build code blocks for each relation type
157 $columns = !empty($rendered) ? implode("\n ", $rendered) : '';
158
159 // Assign to stub placeholders
160 $inserts['{{ fieldsInsertPoint }}'] = $columns;
161
162 return $inserts;
163 }
77 } 164 }