diff 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
line wrap: on
line diff
--- a/src/Generator/View/CreateEditViewGenerator.php	Sun May 11 21:03:51 2025 -0400
+++ b/src/Generator/View/CreateEditViewGenerator.php	Thu May 15 21:50:38 2025 -0400
@@ -4,6 +4,8 @@
 
 use Symfony\Component\Console\Attribute\AsCommand;
 use Wizard\MagicForger\Generator\BaseGenerator;
+use Wizard\MagicForger\Helpers\RelationshipNavigator;
+use Illuminate\Support\Str;
 
 #[AsCommand(name: 'mf:create_edit_view')]
 class CreateEditViewGenerator extends BaseGenerator
@@ -74,4 +76,89 @@
     {
         return str_replace(['Resources\\', '\\'], ['resources/', '/'], $this->getViewNamespace($this->getTableInput()).'create_edit.blade.php');
     }
+
+		protected function renderColumns() {
+				$renders = [];
+
+				$columns = $this->getTableColumns($this->getCurrentTable());
+        $relations = RelationshipNavigator::getRelations($this->getCurrentTable());
+				//gether the select columns based on the relations
+				$selects = [];
+
+				foreach($relations['belongsTo'] as $relation) {
+					$selects[$relation['column']] = $relation['table'];
+				}
+
+				foreach($columns as $column) {
+					$name = $column['name'];
+					if(in_array($name, $this->columns_to_ignore)) continue;
+
+					$type = $column['type_name'];
+
+					$replacements = [
+						'{{fieldName}}' => $name,
+						'{{fieldLabel}}' => Str::headline($name),
+						'{{required}}' => $column['nullable'] ? 'false' : 'true',
+					];
+					$snippet = '';
+
+					//date
+					if(in_array($type, ['date', 'timestamp'])) {
+						$snippet = $this->getSnippet('input/date');	
+					}
+					//checkbox
+					elseif(in_array($type, ['tinyint']) && array_key_exists($name, $selects)) {
+						$snippet = $this->getSnippet('input/checkbox');	
+					}
+
+					//select
+					elseif(in_array($type, ['bigint']) && array_key_exists($name, $selects)) {
+						$snippet = $this->getSnippet('input/select');	
+						$replacements['{{fieldLabel}}'] = Str::headline(Str::singular($selects[$name]));
+						$replacements['{{options}}'] = '$'.$selects[$name];
+					}
+
+					//text area
+					elseif(in_array($type, ['text'])) {
+						$snippet = $this->getSnippet('input/textarea');	
+					}
+					else {
+						//varchar, bigint, float, etc
+						$snippet = $this->getSnippet('input/text');	
+					}
+
+
+					// Replace placeholders with actual values
+        	$renders[] = str_replace(
+							array_keys($replacements),
+							$replacements,
+        	    $snippet
+        	);
+				}
+			return $renders;
+		}
+
+		
+
+    /**
+     * Get available insertions including model relationships.
+     *
+     * @return array
+     */
+    public function get_available_inserts(): array
+    {
+        // Merge parent insertions (attributes, fillable, etc.)
+        $inserts = parent::get_available_inserts();
+
+        // Gather and render relationships for this model
+        $rendered = $this->renderColumns();
+
+        // Build code blocks for each relation type
+        $columns = !empty($rendered) ? implode("\n    ", $rendered) : '';
+
+        // Assign to stub placeholders
+        $inserts['{{ fieldsInsertPoint }}'] = $columns;
+
+        return $inserts;
+    }
 }