浏览代码

Support for col-spec definitions for listing pages

Vijayakrishnan 5 年之前
父节点
当前提交
0694fc86f4
共有 2 个文件被更改,包括 58 次插入4 次删除
  1. 55 4
      app/Console/Commands/GenerateTreeCommand.php
  2. 3 0
      generatecv/tree.txt

+ 55 - 4
app/Console/Commands/GenerateTreeCommand.php

@@ -175,6 +175,11 @@ class GenerateTreeCommand extends Command
                                 $currentMethod->viewLinkField = substr($line, 5);
                             }
                         }
+                        else if(strpos($line, "!col:") === 0) { // !col:
+                            if (!empty($currentMethod)) {
+                                $currentMethod->setColumnSpec(substr($line, 5), $exitURL);
+                            }
+                        }
                         else if (!empty($currentMethod) &&
                             (strpos($currentMethod->name, 'add_new') === 0 ||
                                 $currentMethod->name === 'remove')) { // this is a field in add_new
@@ -229,6 +234,11 @@ class GenerateTreeCommand extends Command
                                     $currentMethod->viewLinkField = substr($line, 5);
                                 }
                             }
+                            else if(strpos($line, "!col:") === 0) { // !col:
+                                if (!empty($currentMethod)) {
+                                    $currentMethod->setColumnSpec(substr($line, 5), $exitURL);
+                                }
+                            }
                             else if(!empty($currentMethod)) {
                                 $currentMethod->data[] = $line;
                                 if($exitURL) {
@@ -498,11 +508,30 @@ class GenController {
         }
 
         foreach ($columns as $column) {
-            $ths[] = "<th>{$this->snakeToTitleCase($column)}</th>";
+
+            $columnTitle = $this->snakeToTitleCase($column);
+            $columnValue = "<?= \$record->$column ?>";
+            $hasLink = $controller->hasView && $column === $method->viewLinkField;
+            $linkTarget = "/{$controller->name}/view/<?= \$record->uid ?>";
+
+            // check if this column has column spec
+            if(isset($method->columns[$column])) {
+                $columnTitle = $method->columns[$column]["label"];
+                if(isset($method->columns[$column]["query"])) {
+                    $columnValue = "<?= \Illuminate\Support\Facades\DB::" .
+                        "select(\"{$method->columns[$column]["query"]}\")[0]->result ?>";
+                }
+                if(isset($method->columns[$column]["link"])) {
+                    $hasLink = true;
+                    $linkTarget = $method->columns[$column]["link"];
+                }
+            }
+
+            $ths[] = "<th>$columnTitle</th>";
             $tds[] = "<td>" .
-                ($controller->hasView && $column === $method->viewLinkField ? '<a href="/' . $controller->name . '/view/<?= $record->uid ?>">' : '') .
-                "<?= \$record->$column ?>" .
-                ($controller->hasView && $column === $method->viewLinkField ? '</a>' : '') .
+                ($hasLink ? "<a href=\"{$linkTarget}\">" : "") .
+                $columnValue .
+                ($hasLink ? "</a>" : "") .
                 "</td>";
         }
         $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
@@ -844,6 +873,7 @@ class GenControllerMethod {
     public $incType = null;
     public $incFields = null;
     public $viewLinkField = 'uid';
+    public $columns = [];
     public function __construct($name, $route)
     {
         $this->name = $name;
@@ -862,4 +892,25 @@ class GenControllerMethod {
             }
         }
     }
+    public function setColumnSpec($line, $link) {
+        // ally_pro_id:Ally Pro:select name_display as 'result' from pro where id = $ally_pro_id limit 1
+        /*
+        label
+        query
+        link
+        */
+        $parts = explode(":", $line);
+        $spec = [
+            "label" => $parts[1]
+        ];
+        if(count($parts) > 2) {
+            $query = $parts[2];
+            $query = preg_replace("/\\$(\S+)/", "{\$record->$1}", $query);
+            $spec['query'] = $query;
+        }
+        if($link) {
+            $spec['link'] = preg_replace("/\\$(\S+)/", "<?= \$record->$1 ?>", $link);
+        }
+        $this->columns[$parts[0]] = $spec;
+    }
 }

+ 3 - 0
generatecv/tree.txt

@@ -3,6 +3,9 @@ PRO
     my_payment_schedule|pro_rate:pro_id=OWN
     my_teams|team|add|view
         !inc:is_active,@team_number,client_count,hcp_pro_id,ally_pro_id
+        !col:hcp_pro_id:HCP Pro: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
+        !col:client_count:Clients:select '- TODO -' as result
     my_teams/add_new:create
         hcpProUid:record:pro:uid,name_display
         allyProUid:record:pro:uid,name_display