Sfoglia il codice sorgente

Support for col-spec definitions for sub-listing pages

Vijayakrishnan 5 anni fa
parent
commit
e64aff8cd3
2 ha cambiato i file con 30 aggiunte e 9 eliminazioni
  1. 29 9
      app/Console/Commands/GenerateTreeCommand.php
  2. 1 0
      generatecv/tree.txt

+ 29 - 9
app/Console/Commands/GenerateTreeCommand.php

@@ -177,7 +177,7 @@ class GenerateTreeCommand extends Command
                         }
                         else if(strpos($line, "!col:") === 0) { // !col:
                             if (!empty($currentMethod)) {
-                                $currentMethod->setColumnSpec(substr($line, 5), $exitURL);
+                                $currentMethod->setColumnSpec(substr($line, 5), $exitURL, 'record');
                             }
                         }
                         else if (!empty($currentMethod) &&
@@ -236,7 +236,7 @@ class GenerateTreeCommand extends Command
                             }
                             else if(strpos($line, "!col:") === 0) { // !col:
                                 if (!empty($currentMethod)) {
-                                    $currentMethod->setColumnSpec(substr($line, 5), $exitURL);
+                                    $currentMethod->setColumnSpec(substr($line, 5), $exitURL, 'subRecord');
                                 }
                             }
                             else if(!empty($currentMethod)) {
@@ -671,12 +671,32 @@ class GenController {
         }
 
         foreach ($columns as $column) {
-            $ths[] = "<th>{$this->snakeToTitleCase($column)}</th>";
+
+            $columnTitle = $this->snakeToTitleCase($column);
+            $columnValue = "<?= \$subRecord->$column ?>";
+            $hasLink = $method->exitURL && $column === $method->viewLinkField;
+            $linkTarget = $method->exitURL;
+
+            // 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>" .
-                ($method->exitURL && $column === $method->viewLinkField ? '<a href="' . $method->exitURL . '">' : '') .
-                "<?= \$subRecord->$column ?>" .
-                ($method->exitURL && $column === $method->viewLinkField ? '</a>' : '') .
+                ($hasLink ? "<a href=\"{$linkTarget}\">" : "") .
+                $columnValue .
+                ($hasLink ? "</a>" : "") .
                 "</td>";
+
         }
         $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
         $text = str_replace("<!-- __SCAFFOLD_TDS__ -->", implode("\n", $tds), $text);
@@ -892,7 +912,7 @@ class GenControllerMethod {
             }
         }
     }
-    public function setColumnSpec($line, $link) {
+    public function setColumnSpec($line, $link, $recordVariable) {
         // ally_pro_id:Ally Pro:select name_display as 'result' from pro where id = $ally_pro_id limit 1
         /*
         label
@@ -905,11 +925,11 @@ class GenControllerMethod {
         ];
         if(count($parts) > 2) {
             $query = $parts[2];
-            $query = preg_replace("/\\$(\S+)/", "{\$record->$1}", $query);
+            $query = preg_replace("/\\$(\S+)/", "{\$$recordVariable->$1}", $query);
             $spec['query'] = $query;
         }
         if($link) {
-            $spec['link'] = preg_replace("/\\$(\S+)/", "<?= \$record->$1 ?>", $link);
+            $spec['link'] = preg_replace("/\\$(\S+)/", "<?= \$$recordVariable->$1 ?>", $link);
         }
         $this->columns[$parts[0]] = $spec;
     }

+ 1 - 0
generatecv/tree.txt

@@ -28,6 +28,7 @@ PRO
                 id=client.team_id=>/my_clients/view/UID
                 !exc:id,uid
                 !lnk:created_at
+                !col:team_id:Team:select team_number as result from team where id = $team_id limit 1
                 add_new:client
                     teamUid:hidden=uid
                     nameDisplay