Prechádzať zdrojové kódy

Support for re-usable queries in form <select> fields

Vijayakrishnan Krishnan 5 rokov pred
rodič
commit
d4e419b1de

+ 32 - 5
app/Console/Commands/GenerateTreeCommand.php

@@ -52,6 +52,11 @@ class GenerateTreeCommand extends Command
 
         foreach ($lines as $line) {
 
+            // stop (debugging)
+            if(trim($line) === '##stop') {
+                exit(0);
+            }
+
             // skip comments
             if(trim($line)[0] === '#') continue;
 
@@ -468,9 +473,20 @@ class GenController {
                         $loadingLine[] = "->where('{$this->condition['field']}', {$this->condition['value']})";
                     }
                     $loadingLine[] = "->get();";
+                    $input[] = "'records'";
                     $code[] = implode("", $loadingLine);
+
+                    foreach ($method->queries as $key => $query) {
+                        // replace $x with " . ($record->x ? $record->x : "''") . "
+                        $query = preg_replace("/([^$])\\$([a-zA-Z0-9_]+)/", "$1\" . (\$record->$2 ? \$record->$2 : \"''\") . \"", $query);
+                        // replace $$x with " . ($subRecord->x ? $subRecord->x : "''") . "
+                        $query = preg_replace("/\\$\\$([a-zA-Z0-9_]+)/", "\" . (\$subRecord->$1 ? \$subRecord->$1 : \"''\") . \"", $query);
+                        $code[] = "\t\t\$result_$key = DB::select(\"$query\");";
+                        $input[] = "'result_$key'";
+                    }
+
                     $code[] = "\t\treturn response()->view('{$this->root}/{$this->name}/{$method->name}', " .
-                        "compact('records'), " .
+                        "compact(" . implode(", ", $input) . "), " .
                         "session('message') ? 500 : 200)->header('Content-Type', 'text/html');";
                 }
             }
@@ -569,6 +585,9 @@ class GenController {
                         "select(\"{$method->columns[$column]["query"]}\");\n" .
                         "echo (\$_r && count(\$_r)) ? \$_r[0]->result : '-'; ?>";
                 }
+                else if(isset($method->columns[$column]["getter"])) {
+                    $columnValue = $method->columns[$column]["getter"];
+                }
                 if(isset($method->columns[$column]["link"])) {
                     $hasLink = true;
                     $linkTarget = $method->columns[$column]["link"];
@@ -915,8 +934,10 @@ class GenController {
             $this->w('Rout: ' . $method->route, 1);
             $this->w('Meth: ' . $method->name . '($request' . ($method->hasUID ? ', $uid' : '') . ')', 1);
             if(!empty($method->data)) $this->w('Data: ' . implode(", ", $method->data), 1);
-            $this->w('Exit: ' . $method->exitURL, 1);
-            $this->w('View: ' . $method->viewURL, 1);
+//            $this->w('Exit: ' . $method->exitURL, 1);
+//            $this->w('View: ' . $method->viewURL, 1);
+            dump($method->columns);
+            dump($method->queries);
             if(!$method->redirect) {
                 $this->w('View: ' . resource_path("views/{$this->root}/{$this->name}/{$method->name}.blade.php"), 1);
             }
@@ -1018,8 +1039,14 @@ class GenController {
                     ($required ? "required" : "") .
                     ">";
                 $code[] = "<option value=''>-- Select --</option>";
-                $code[] = "<?php \$dbOptions = \Illuminate\Support\Facades\DB::table('{$options['table']}')->get(); ?>";
-                $code[] = "<?php foreach(\$dbOptions as \$o): ?>";
+                if($options['table'][0] === '~') { // from named query
+                    $code[] = "<?php foreach(\$result_" . substr($options['table'], 1) . " as \$o): ?>";
+                }
+                else { // select query
+                    $code[] = "<?php \$dbOptions = \Illuminate\Support\Facades\DB::table('{$options['table']}')->get(); ?>";
+                    $code[] = "<?php foreach(\$dbOptions as \$o): ?>";
+                }
+
                 $code[] = "<option " .
                     "<?= \$o->{$options['valueField']} === (old('$name') ? old('$name') : " . ($default ? "\$record->$default" : "''") . ") ? 'selected' : '' ?> " .
                     "value='<?= \$o->{$options['valueField']} ?>'><?= \$o->{$options['displayField']} ?> (<?= \$o->{$options['valueField']} ?>)</option>";

+ 6 - 4
generatecv/tree.txt

@@ -8,12 +8,14 @@ PRO
         !col:earnings:Earnings:select '- TODO -' as result
     my_teams|team|add|view|icon:user-friends
         !inc:@team_number,hcp_pro_id,ally_pro_id,client_count
-        !col:hcp_pro_id:HCP:select name_display as result from pro where id = $hcp_pro_id limit 1=>/foo/bar/$hcp_pro_id
-        !col:ally_pro_id:Ally Pro:select name_display as result from pro where id = $ally_pro_id limit 1
+        !qry:pros:select id, name_display from pro
+        !col:hcp_pro_id:HCP:~pros:name_display:id,=,$hcp_pro_id:all
+        !col:ally_pro_id:Ally Pro:~pros:name_display:id,=,$ally_pro_id:all
         !col:client_count:Clients:select COUNT(*) as result FROM client WHERE team_id = $id
     my_teams/add_new:create
-        hcpProUid*:record:pro:uid,name_display
-        allyProUid*:record:pro:uid,name_display
+        !qry:pros:select uid, name_display from pro
+        hcpProUid*:record:~pros:uid,name_display
+        allyProUid*:record:~pros:uid,name_display
         teamNumber
     my_teams/view/{uid}
         ACTIONS