Преглед изворни кода

Support for !inc/!exc for listing fields

Vijayakrishnan пре 5 година
родитељ
комит
ac627c0b38
2 измењених фајлова са 83 додато и 15 уклоњено
  1. 79 14
      app/Console/Commands/GenerateTreeCommand.php
  2. 4 1
      generatecv/tree.txt

+ 79 - 14
app/Console/Commands/GenerateTreeCommand.php

@@ -139,7 +139,7 @@ class GenerateTreeCommand extends Command
                             $currentController->hasAdd = $hasAdd;
                             $currentController->hasView = $hasView;
                             $currentController->hasRemove = $hasRemove;
-                            $currentController->addMethod("index", "/$line");
+                            $currentMethod = $currentController->addMethod("index", "/$line");
                             if(!empty($currentSubController)) {
                                 $currentSubController->save();
                             }
@@ -155,11 +155,26 @@ class GenerateTreeCommand extends Command
                         }
                         break;
 
-                    case 8: // sub-type declaration | add_new fields
+                    case 8: // sub-type declaration | add_new fields | !inc/!exc
 
                         if($line === 'ACTIONS' || $line === 'SUB') {
                             $currentSubType = $line;
                         }
+                        else if(strpos($line, "!inc:") === 0) { // !inc:
+                            if (!empty($currentMethod)) {
+                                $currentMethod->setIncFields('include', explode(",", substr($line, 5)));
+                            }
+                        }
+                        else if(strpos($line, "!exc:") === 0) { // !exc:
+                            if (!empty($currentMethod)) {
+                                $currentMethod->setIncFields('exclude', explode(",", substr($line, 5)));
+                            }
+                        }
+                        else if(strpos($line, "!lnk:") === 0) { // !lnk:
+                            if (!empty($currentMethod)) {
+                                $currentMethod->viewLinkField = substr($line, 5);
+                            }
+                        }
                         else if (!empty($currentMethod) &&
                             (strpos($currentMethod->name, 'add_new') === 0 ||
                                 $currentMethod->name === 'remove')) { // this is a field in add_new
@@ -199,17 +214,32 @@ class GenerateTreeCommand extends Command
                         break;
 
                     case 16: // data for actions and subs
-                        if(!empty($currentMethod)) {
-                            $currentMethod->data[] = $line;
-                            if($exitURL) {
-                                if(strpos("=", $line) !== FALSE) {
-                                    $currentMethod->viewURL = $exitURL;
+                            if(strpos($line, "!inc:") === 0) { // !inc:
+                                if (!empty($currentMethod)) {
+                                    $currentMethod->setIncFields('include', explode(",", substr($line, 5)));
                                 }
-                                else {
-                                    $currentMethod->exitURL = $exitURL;
+                            }
+                            else if(strpos($line, "!exc:") === 0) { // !exc:
+                                if (!empty($currentMethod)) {
+                                    $currentMethod->setIncFields('exclude', explode(",", substr($line, 5)));
+                                }
+                            }
+                            else if(strpos($line, "!lnk:") === 0) { // !lnk:
+                                if (!empty($currentMethod)) {
+                                    $currentMethod->viewLinkField = substr($line, 5);
+                                }
+                            }
+                            else if(!empty($currentMethod)) {
+                                $currentMethod->data[] = $line;
+                                if($exitURL) {
+                                    if(strpos("=", $line) !== FALSE) {
+                                        $currentMethod->viewURL = $exitURL;
+                                    }
+                                    else {
+                                        $currentMethod->exitURL = $exitURL;
+                                    }
                                 }
                             }
-                        }
                         break;
 
                     case 20: // SUB add_new fields
@@ -456,12 +486,23 @@ class GenController {
                 "<i class='fa fa-trash'></i>" .
                 "</a></td>";
         }
+
+        // !inc/!exc
+        if($method->incType === "include") {
+            $columns = $method->incFields;
+        }
+        else if($method->incType === "exclude") {
+            $columns = array_filter($columns, function($item) use ($method) {
+                return in_array($item, $method->incFields) === FALSE;
+            });
+        }
+
         foreach ($columns as $column) {
             $ths[] = "<th>{$this->snakeToTitleCase($column)}</th>";
             $tds[] = "<td>" .
-                ($controller->hasView && $column === 'uid' ? '<a href="/' . $controller->name . '/view/<?= $record->uid ?>">' : '') .
+                ($controller->hasView && $column === $method->viewLinkField ? '<a href="/' . $controller->name . '/view/<?= $record->uid ?>">' : '') .
                 "<?= \$record->$column ?>" .
-                ($controller->hasView && $column === 'uid' ? '</a>' : '') .
+                ($controller->hasView && $column === $method->viewLinkField ? '</a>' : '') .
                 "</td>";
         }
         $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
@@ -589,12 +630,23 @@ class GenController {
 
         $ths = [];
         $tds = [];
+
+        // !inc/!exc
+        if($method->incType === "include") {
+            $columns = $method->incFields;
+        }
+        else if($method->incType === "exclude") {
+            $columns = array_filter($columns, function($item) use ($method) {
+                return in_array($item, $method->incFields) === FALSE;
+            });
+        }
+
         foreach ($columns as $column) {
             $ths[] = "<th>{$this->snakeToTitleCase($column)}</th>";
             $tds[] = "<td>" .
-                ($method->exitURL && $column === 'uid' ? '<a href="' . $method->exitURL . '">' : '') .
+                ($method->exitURL && $column === $method->viewLinkField ? '<a href="' . $method->exitURL . '">' : '') .
                 "<?= \$subRecord->$column ?>" .
-                ($method->exitURL && $column === 'uid' ? '</a>' : '') .
+                ($method->exitURL && $column === $method->viewLinkField ? '</a>' : '') .
                 "</td>";
         }
         $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
@@ -789,6 +841,9 @@ class GenControllerMethod {
     public $viewURL = false;
     public $exitURL = false;
     public $showLink = true;
+    public $incType = null;
+    public $incFields = null;
+    public $viewLinkField = 'uid';
     public function __construct($name, $route)
     {
         $this->name = $name;
@@ -797,4 +852,14 @@ class GenControllerMethod {
             $this->hasUID = true;
         }
     }
+    public function setIncFields($type, $fields) {
+        $this->incType = $type;
+        $this->incFields = $fields;
+        for ($i = 0; $i < count($this->incFields); $i++) {
+            if($this->incFields[$i][0] === '@') {
+                $this->incFields[$i] = substr($this->incFields[$i], 1);
+                $this->viewLinkField = $this->incFields[$i];
+            }
+        }
+    }
 }

+ 4 - 1
generatecv/tree.txt

@@ -2,6 +2,7 @@ PRO
 #    dashboard
     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
     my_teams/add_new:create
         hcpProUid:record:pro:uid,name_display
         allyProUid:record:pro:uid,name_display
@@ -22,6 +23,8 @@ PRO
             dashboard
             clients
                 id=client.team_id=>/my_clients/view/UID
+                !exc:id,uid
+                !lnk:created_at
                 add_new:client
                     teamUid:hidden=uid
                     nameDisplay
@@ -646,4 +649,4 @@ ADMIN
     meeting_messages|meeting_message|view
     meeting_message/view/{uid}
         SUB
-            dashboard
+            dashboard