Josh Kamau 5 anni fa
parent
commit
912328d628

+ 146 - 13
app/Console/Commands/GenerateTreeCommand.php

@@ -236,7 +236,19 @@ class GenerateTreeCommand extends Command
                             }
                             else if(strpos($line, "!col:") === 0) { // !col:
                                 if (!empty($currentMethod)) {
-                                    $currentMethod->setColumnSpec(substr($line, 5), $exitURL, 'subRecord');
+                                    $currentMethod->setColumnSpec(substr($line, 5), $exitURL,
+                                        $currentMethod->name === 'SUB_dashboard' ? 'record' : 'subRecord'
+                                    );
+                                }
+                            }
+                            else if(strpos($line, "!grp:") === 0) { // !grp:
+                                if (!empty($currentMethod)) {
+                                    $currentMethod->addGroup(substr($line, 5));
+                                }
+                            }
+                            else if(strpos($line, "!act:") === 0) { // !act:
+                                if (!empty($currentMethod)) {
+                                    $currentMethod->addColumnAction(substr($line, 5), $exitURL);
                                 }
                             }
                             else if(!empty($currentMethod)) {
@@ -705,7 +717,90 @@ class GenController {
     }
     public function generateSubContent(GenController $controller, GenControllerMethod $method, $text) {
         if($method->name === 'SUB_dashboard') {
-            $html = file_get_contents(base_path('generatecv/tree-templates/dashboard.template.blade.php'));
+            if(!isset($method->groups) || !count($method->groups)) {
+                $html = file_get_contents(base_path('generatecv/tree-templates/dashboard.template.blade.php'));
+            }
+            else {
+                $html = file_get_contents(base_path('generatecv/tree-templates/dashboard-grouped.template.blade.php'));
+                $groupsHtml = [];
+                $groupTemplate = file_get_contents(base_path('generatecv/tree-templates/dashboard-group.template.blade.php'));
+                foreach ($method->groups as $group) {
+                    $groupHtml = $groupTemplate;
+                    $groupHtml = str_replace("<!-- __GROUP_NAME__ -->", $group["name"], $groupHtml);
+                    $fields = [];
+                    foreach ($group["fields"] as $field) {
+
+                        $columnTitle = $this->snakeToTitleCase($field);
+                        $columnValue = "<?= \$record->$field ?>";
+                        $hasLink = false;
+                        $linkTarget = null;
+                        $actions = [];
+
+                        // check if this column has column spec
+                        if(isset($method->columns[$field])) {
+                            $columnTitle = $method->columns[$field]["label"];
+                            if(isset($method->columns[$field]["query"])) {
+                                $columnValue = "<?= \Illuminate\Support\Facades\DB::" .
+                                    "select(\"{$method->columns[$field]["query"]}\")[0]->result ?>";
+                            }
+                            if(isset($method->columns[$field]["link"])) {
+                                $hasLink = true;
+                                $linkTarget = $method->columns[$field]["link"];
+                            }
+                        }
+
+                        /*
+                        "is_active" => array:1 [
+                            0 => array:5 [
+                            "label" => "Deactivate"
+                              "icon" => "edit"
+                              "condition" => "if"
+                              "field" => "is_active"
+                            ]
+                        ]
+                        */
+                        if(isset($method->columnActions[$field])) {
+                            foreach($method->columnActions[$field] as $action) {
+                                $actionLine = [];
+                                if(isset($action["condition"])) {
+                                    if($action["condition"] === "if") {
+                                        $actionLine[] = "@if(";
+                                    }
+                                    else {
+                                        $actionLine[] = "@if(!";
+                                    }
+                                    $actionLine[] = "\$record->{$action["field"]})";
+                                }
+                                $actionLine[] = "<a " .
+                                    'up-modal=".form-contents" up-width="800" up-history="false" ' .
+                                    "href='{$action["link"]}' title='{$action["label"]}' class='ml-2 text-dark font-weight-normal'>" .
+                                    "<i class='fa fa-{$action["icon"]} text-sm'></i>" .
+                                    "</a>";
+                                if(isset($action["condition"])) {
+                                    $actionLine[] = "@endif";
+                                }
+                                $actionLine = implode(" ", $actionLine);
+                                $actions[] = $actionLine;
+                            }
+                        }
+                        $actions = implode("\n", $actions);
+
+                        $fields[] = "<tr>" .
+                            "<td class=\"w-25 px-2 text-secondary border-right\">$columnTitle</td>" .
+                            "<td class=\"w-75 px-2 font-weight-bold\">" .
+                                ($hasLink ? "<a href=\"{$linkTarget}\">" : "") .
+                                $columnValue .
+                                ($hasLink ? "</a>" : "") .
+                                $actions .
+                            "</td>" .
+                        "</tr>";
+                    }
+                    $groupHtml = str_replace("<!-- __GROUP_FIELDS__ -->", implode("\n", $fields), $groupHtml);
+                    $groupsHtml[] = $groupHtml;
+                }
+                $groupsHtml = implode("\n", $groupsHtml);
+                $html = str_replace("<!-- _GROUPS_ -->", $groupsHtml, $html);
+            }
             $text = str_replace("_SUB_VIEW_", $html, $text);
             $text = str_replace("_ACTION_LINKS_VIEW_", "{$controller->root}/{$controller->parentControllerName}/actions", $text);
         }
@@ -808,6 +903,11 @@ class GenController {
         }
         $tokens = explode(":", $tokens[0]);
         $name = $tokens[0];
+        $required = false;
+        if($name[strlen($name) - 1] === '*') { // required field?
+            $required = true;
+            $name = substr($name, 0, strlen($name) - 1);
+        }
         $display = $name;
         $dotPos = strpos($name, ".");
         if($dotPos !== FALSE) {
@@ -832,12 +932,15 @@ class GenController {
         }
         if($type !== 'hidden' && $type !== 'bool') {
             $code[] = "<div class='form-group mb-3'>";
-            $code[] = "<label class='control-label'>{$this->camelToTitleCase($this->snakeToTitleCase($display))}</label>";
+            $code[] = "<label class='control-label'>{$this->camelToTitleCase($this->snakeToTitleCase($display))} " .
+                ($required ? "*" : "") .
+                "</label>";
         }
         $valueLine = "value='{{ old('$name') ? old('$name') : " . ($default ? "\$record->$default" : '\'\'') . " }}' ";
         switch ($type) {
             case "select":
                 $code[] = "<select class='form-control' name='$name' " . $valueLine .
+                    ($required ? "required" : "") .
                     ">";
                 $code[] = "<option value=''>-- Select --</option>";
                 foreach ($options as $o) {
@@ -849,6 +952,7 @@ class GenController {
                 break;
             case "record":
                 $code[] = "<select class='form-control' name='$name' " . $valueLine .
+                    ($required ? "required" : "") .
                     ">";
                 $code[] = "<option value=''>-- Select --</option>";
                 $code[] = "<?php \$dbOptions = \Illuminate\Support\Facades\DB::table('{$options['table']}')->get(); ?>";
@@ -861,12 +965,17 @@ class GenController {
                 break;
             case "bool":
                 $code[] = "<div class='form-group mb-3'>";
-                $code[] = "<label class='control-label'>{$this->camelToTitleCase($this->snakeToTitleCase($display))} ";
-                $code[] = "<input class='ml-2' type='checkbox' name='$name'>";
+                $code[] = "<label class='control-label'>{$this->camelToTitleCase($this->snakeToTitleCase($display))} " .
+                    ($required ? "*" : "");
+                $code[] = "<input class='ml-2' type='checkbox' name='$name' " .
+                    ($required ? "required" : "") .
+                    ">";
                 $code[] = "</label>";
                 break;
             default:
-                $code[] = "<input class='form-control' type='$type' name='$name' " . $valueLine . ">";
+                $code[] = "<input class='form-control' type='$type' name='$name' " . $valueLine .
+                    ($required ? "required" : "") .
+                    ">";
         }
         if($type !== 'hidden') {
             $code[] = "</div>";
@@ -894,6 +1003,8 @@ class GenControllerMethod {
     public $incFields = null;
     public $viewLinkField = 'uid';
     public $columns = [];
+    public $columnActions = [];
+    public $groups = [];
     public function __construct($name, $route)
     {
         $this->name = $name;
@@ -914,23 +1025,45 @@ class GenControllerMethod {
     }
     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
-        query
-        link
-        */
         $parts = explode(":", $line);
         $spec = [
             "label" => $parts[1]
         ];
         if(count($parts) > 2) {
             $query = $parts[2];
-            $query = preg_replace("/\\$(\S+)/", "{\$$recordVariable->$1}", $query);
+            $query = preg_replace("/\\$([a-zA-Z0-9_]+)/", "{\$$recordVariable->$1}", $query);
             $spec['query'] = $query;
         }
         if($link) {
-            $spec['link'] = preg_replace("/\\$(\S+)/", "<?= \$$recordVariable->$1 ?>", $link);
+            $spec['link'] = preg_replace("/\\$([a-zA-Z0-9_]+)/", "<?= \$$recordVariable->$1 ?>", $link);
         }
         $this->columns[$parts[0]] = $spec;
     }
+    public function addGroup($line) {
+        // Basic Details:id,uid,created_at,clients_count
+        $parts = explode(":", $line);
+        $this->groups[] = [
+            "name" => $parts[0],
+            "fields" => explode(",", $parts[1])
+        ];
+    }
+    public function addColumnAction($line, $link) {
+        // is_active:Deactivate:deactivate:edit:if:is_active
+        $parts = explode(":", $line);
+        $action = [
+            "label" => $parts[1],
+            "icon" => isset($parts[2]) ? $parts[2] : 'edit'
+        ];
+        if(count($parts) >= 5 ) {
+            $action["condition"] = $parts[3];
+            $action["field"] = $parts[4];
+        }
+        if($link) {
+            $action['link'] = preg_replace("/\\$([a-zA-Z0-9_]+)/", "<?= \$record->$1 ?>", $link);
+        }
+        if(!isset($this->columnActions[$parts[0]])) {
+            $this->columnActions[$parts[0]] = [];
+        }
+        $this->columnActions[$parts[0]][] = $action;
+    }
 }

+ 12 - 0
generatecv/tree-templates/dashboard-group.template.blade.php

@@ -0,0 +1,12 @@
+<div class="table-responsive p-0 bg-white table-sm my-3">
+    <table class="table table-hover text-nowrap table-striped border-left border-right border-bottom">
+        <thead>
+        <tr>
+            <th colspan="2" class="px-2"><!-- __GROUP_NAME__ --></th>
+        </tr>
+        </thead>
+        <tbody>
+            <!-- __GROUP_FIELDS__ -->
+        </tbody>
+    </table>
+</div>

+ 12 - 0
generatecv/tree-templates/dashboard-grouped.template.blade.php

@@ -0,0 +1,12 @@
+<div class="row">
+    <div class="col-7">
+
+        <!-- _GROUPS_ -->
+
+    </div>
+    <div class="col-5">
+        <div class="border-left h-100 pt-3 px-3">
+            @include('_ACTION_LINKS_VIEW_')
+        </div>
+    </div>
+</div>

+ 18 - 9
generatecv/tree.txt

@@ -12,8 +12,8 @@ PRO
         !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 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
+        hcpProUid*:record:pro:uid,name_display
+        allyProUid*:record:pro:uid,name_display
         teamNumber
     my_teams/view/{uid}
         ACTIONS
@@ -29,6 +29,15 @@ PRO
                 memo=reactivation_memo
         SUB
             dashboard
+                !grp:Basic Details:team_number,created_at,client_count,is_active
+                !grp:Associated Pros: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
+                !col:is_active:Active:SELECT d as result FROM (VALUES ('1','Yes'),('','No')) T(v,d) WHERE v = '$is_active' limit 1
+                !act:team_number:Update Team Number:pencil-alt=>/my_teams/view/$uid/ACTION_updateTeamNumber
+                !act:is_active:Deactivate:pencil-alt:if:is_active=>/my_teams/view/$uid/ACTION_deactivate
+                !act:is_active:Reactivate:pencil-alt:if-not:is_active=>/my_teams/view/$uid/ACTION_reactivate
             clients
                 id=client.team_id=>/my_clients/view/UID
                 !exc:id,uid
@@ -36,17 +45,17 @@ PRO
                 !col:team_id:Team:select team_number as result from team where id = $team_id limit 1
                 add_new:client
                     teamUid:hidden=uid
-                    nameDisplay
+                    nameDisplay*
                     namePrefix
-                    nameFirst
+                    nameFirst*
                     nameMiddle
-                    nameLast
+                    nameLast*
                     nameSuffix
                     nameCredential
-                    gender:select:M,F
-                    dateOfBirth:date
-                    cellNumber:tel
-                    emailAddress:email
+                    gender*:select:M,F
+                    dateOfBirth*:date
+                    cellNumber*:tel
+                    emailAddress*:email
                     medicareNumber
             audit_log
     my_clients|client|add|view