Quellcode durchsuchen

Support for fieldsets and actions in dashboard

Vijayakrishnan vor 5 Jahren
Ursprung
Commit
ff23d2121b

+ 128 - 9
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);
         }
@@ -894,6 +989,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 +1011,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>

+ 10 - 0
generatecv/tree.txt

@@ -3,6 +3,7 @@ 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:is_active:Active:SELECT d as result FROM (VALUES ('1','Yes'),('','No')) T(v,d) WHERE v = '$is_active' limit 1
         !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
@@ -24,6 +25,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