فهرست منبع

Support for re-usable queries for column values

Vijayakrishnan Krishnan 5 سال پیش
والد
کامیت
a7271df838

+ 76 - 3
app/Console/Commands/GenerateTreeCommand.php

@@ -185,6 +185,11 @@ class GenerateTreeCommand extends Command
                                 $currentMethod->setColumnSpec(substr($line, 5), $exitURL, 'record');
                                 $currentMethod->setColumnSpec(substr($line, 5), $exitURL, 'record');
                             }
                             }
                         }
                         }
+                        else if(strpos($line, "!qry:") === 0) { // !qry:
+                            if (!empty($currentMethod)) {
+                                $currentMethod->addQuery(substr($line, 5));
+                            }
+                        }
                         else if (!empty($currentMethod) &&
                         else if (!empty($currentMethod) &&
                             (strpos($currentMethod->name, 'add_new') === 0 ||
                             (strpos($currentMethod->name, 'add_new') === 0 ||
                                 $currentMethod->name === 'remove')) { // this is a field in add_new
                                 $currentMethod->name === 'remove')) { // this is a field in add_new
@@ -246,6 +251,11 @@ class GenerateTreeCommand extends Command
                                     );
                                     );
                                 }
                                 }
                             }
                             }
+                            else if(strpos($line, "!qry:") === 0) { // !qry:
+                                if (!empty($currentMethod)) {
+                                    $currentMethod->addQuery(substr($line, 5));
+                                }
+                            }
                             else if(strpos($line, "!grp:") === 0) { // !grp:
                             else if(strpos($line, "!grp:") === 0) { // !grp:
                                 if (!empty($currentMethod)) {
                                 if (!empty($currentMethod)) {
                                     $currentMethod->addGroup(substr($line, 5), $exitURL);
                                     $currentMethod->addGroup(substr($line, 5), $exitURL);
@@ -398,9 +408,12 @@ class GenController {
                 $code[] = "\t\t" . 'return redirect("' . $target . '");';
                 $code[] = "\t\t" . 'return redirect("' . $target . '");';
             }
             }
             else {
             else {
+
+                $input = [];
+
                 if($method->hasUID) {
                 if($method->hasUID) {
                     $code[] = "\t\t\$record = DB::table('{$this->dbTable}')->where('uid', \$uid)->first();";
                     $code[] = "\t\t\$record = DB::table('{$this->dbTable}')->where('uid', \$uid)->first();";
-                    $input = ["'record'"];
+                    $input[] = "'record'";
                     // if sub-index controller, load subRecords
                     // if sub-index controller, load subRecords
                     if($method->type === 'sub' && count($method->data)) {
                     if($method->type === 'sub' && count($method->data)) {
                         $parts = explode(",", $method->data[0]);
                         $parts = explode(",", $method->data[0]);
@@ -433,6 +446,16 @@ class GenController {
                         // $code[] = "\t\t\$subRecords = DB::table('$foreignTable')->where('$foreignField', \$record->$localField)->get();";
                         // $code[] = "\t\t\$subRecords = DB::table('$foreignTable')->where('$foreignField', \$record->$localField)->get();";
                         $input[] = "'subRecords'";
                         $input[] = "'subRecords'";
                     }
                     }
+
+                    foreach ($method->queries as $key => $query) {
+                        // replace $x with " . ($record->x ? $record->x : "''") . "
+                        $query = preg_replace("/([^$])\\$([a-zA-Z0-9_]+)/", "$1\" . (\$record->$2 ? \$record->$2 : \"''\") . \"", $query);
+                        // replace $$x with " . ($subRecord->x ? $subRecord->x : "''") . "
+                        $query = preg_replace("/\\$\\$([a-zA-Z0-9_]+)/", "\" . (\$subRecord->$1 ? \$subRecord->$1 : \"''\") . \"", $query);
+                        $code[] = "\t\t\$result_$key = DB::select(\"$query\");";
+                        $input[] = "'result_$key'";
+                    }
+
                     // return response()->view('pro/my_teams/add_new', compact('records'), session('message') ? 500 : 200)->header('Content-Type', 'text/html');
                     // return response()->view('pro/my_teams/add_new', compact('records'), session('message') ? 500 : 200)->header('Content-Type', 'text/html');
                     $code[] = "\t\treturn response()->view('{$this->root}/{$this->name}/{$method->name}', " .
                     $code[] = "\t\treturn response()->view('{$this->root}/{$this->name}/{$method->name}', " .
                         "compact(" . implode(", ", $input) . "), " .
                         "compact(" . implode(", ", $input) . "), " .
@@ -790,6 +813,9 @@ class GenController {
                                     "select(\"{$method->columns[$field]["query"]}\");\n" .
                                     "select(\"{$method->columns[$field]["query"]}\");\n" .
                                     "echo (\$_r && count(\$_r)) ? \$_r[0]->result : '-'; ?>";
                                     "echo (\$_r && count(\$_r)) ? \$_r[0]->result : '-'; ?>";
                             }
                             }
+                            else if(isset($method->columns[$field]["getter"])) {
+                                $columnValue = $method->columns[$field]["getter"];
+                            }
                             if(isset($method->columns[$field]["link"])) {
                             if(isset($method->columns[$field]["link"])) {
                                 $hasLink = true;
                                 $hasLink = true;
                                 $linkTarget = $method->columns[$field]["link"];
                                 $linkTarget = $method->columns[$field]["link"];
@@ -1044,6 +1070,7 @@ class GenControllerMethod {
     public $groups = [];
     public $groups = [];
     public $dashboard = false;
     public $dashboard = false;
     public $noActionLinks = false;
     public $noActionLinks = false;
+    public $queries = [];
     public function __construct($name, $route)
     public function __construct($name, $route)
     {
     {
         $this->name = $name;
         $this->name = $name;
@@ -1070,8 +1097,50 @@ class GenControllerMethod {
         ];
         ];
         if(count($parts) > 2) {
         if(count($parts) > 2) {
             $query = $parts[2];
             $query = $parts[2];
-            $query = preg_replace("/\\$([a-zA-Z0-9_]+)/", "\" . ($$recordVariable->$1 ? $$recordVariable->$1 : -1) . \"", $query);
-            $spec['query'] = $query;
+            // check if named query
+            if($query[0] === '~') {
+                // hcp_pro_id:HCP Pro:~pros:name_display:id,=,$hcp_pro_id;is_active,=,true:all
+                $recordSet = '$result_' . substr($query, 1);
+                $field = $parts[3];
+                $checkParts = explode(";", $parts[4]);
+                $checks = [];
+                foreach ($checkParts as $checkPart) {
+                    $checkPart = explode(",", $checkPart);
+                    $value = $checkPart[2];
+                    if(strpos($value, '$$') === 0) {
+                        $value = '$subRecord->' . substr($value, 2);
+                    }
+                    else if(strpos($value, '$') === 0) {
+                        $value = '$record->' . substr($value, 1);
+                    }
+                    $checks[] = [
+                        "field" => $checkPart[0],
+                        "op" => $checkPart[1],
+                        "value" => $value
+                    ];
+                }
+                $condition = 'all';
+                if(count($parts) >= 6) {
+                    $condition = $parts[5];
+                }
+                $checksArray = ["["];
+                foreach ($checks as $check) {
+                    $checksArray[] = "[";
+                    $checksArray[] = "'{$check['field']}'";
+                    $checksArray[] = ", ";
+                    $checksArray[] = "'{$check['op']}'";
+                    $checksArray[] = ", ";
+                    $checksArray[] = "{$check['value']}";
+                    $checksArray[] = "], ";
+                }
+                $checksArray[] = "]";
+                $checks = implode("", $checksArray);
+                $spec['getter'] = "<?= value_from_rs($recordSet, '$field', " . $checks . ", '$condition'); ?>";
+            }
+            else {
+                $query = preg_replace("/\\$([a-zA-Z0-9_]+)/", "\" . ($$recordVariable->$1 ? $$recordVariable->$1 : -1) . \"", $query);
+                $spec['query'] = $query;
+            }
         }
         }
         if($link) {
         if($link) {
             $spec['link'] = preg_replace("/\\$([a-zA-Z0-9_]+)/", "<?= \$$recordVariable->$1 ?>", $link);
             $spec['link'] = preg_replace("/\\$([a-zA-Z0-9_]+)/", "<?= \$$recordVariable->$1 ?>", $link);
@@ -1119,4 +1188,8 @@ class GenControllerMethod {
         }
         }
         return $action;
         return $action;
     }
     }
+    public function addQuery($line) {
+        $parts = explode(":", $line);
+        $this->queries[$parts[0]] = $parts[1];
+    }
 }
 }

+ 74 - 0
app/Helpers/Helper.php

@@ -0,0 +1,74 @@
+<?php
+
+if (!function_exists('value_from_rs')) {
+    function value_from_rs($rs, $field, $checks, $condition = "all") {
+        $result = '-';
+        switch ($condition) {
+            case "all":
+                foreach ($rs as $record) {
+                    $failed = false;
+                    foreach ($checks as $check) {
+                        if($check[1] === '=') {
+                            if(!($record->{$check[0]} == $check[2])) {
+                                $failed = true;
+                                break;
+                            }
+                        }
+                        else if($check[1] === '!=') {
+                            if(!($record->{$check[0]} != $check[2])) {
+                                $failed = true;
+                                break;
+                            }
+                        }
+                    }
+                    if(!$failed) {
+                        $result = $record->$field;
+                        break;
+                    }
+                }
+                break;
+            case "any":
+                foreach ($rs as $record) {
+                    foreach ($checks as $check) {
+                        if($check[1] === '=') {
+                            if($record->{$check[0]} == $check[2]) {
+                                $result = $record->$field;
+                                break;
+                            }
+                        }
+                        else if($check[1] === '!=') {
+                            if($record->{$check[0]} != $check[2]) {
+                                $result = $record->$field;
+                                break;
+                            }
+                        }
+                    }
+                }
+                break;
+            case "none":
+                foreach ($rs as $record) {
+                    $failed = false;
+                    foreach ($checks as $check) {
+                        if($check[1] === '=') {
+                            if($record->{$check[0]} == $check[2]) {
+                                $failed = true;
+                                break;
+                            }
+                        }
+                        else if($check[1] === '!=') {
+                            if($record->{$check[0]} != $check[2]) {
+                                $failed = true;
+                                break;
+                            }
+                        }
+                    }
+                    if(!$failed) {
+                        $result = $record->$field;
+                        break;
+                    }
+                }
+                break;
+        }
+        return $result;
+    }
+}

+ 2 - 1
app/Http/Controllers/my_teams_SINGLE_Controller.php

@@ -43,7 +43,8 @@ class my_teams_SINGLE_Controller extends Controller
 	// GET /my_teams/view/{uid}/SUB_dashboard
 	// GET /my_teams/view/{uid}/SUB_dashboard
 	public function SUB_dashboard(Request $request, $uid) {
 	public function SUB_dashboard(Request $request, $uid) {
 		$record = DB::table('team')->where('uid', $uid)->first();
 		$record = DB::table('team')->where('uid', $uid)->first();
-		return response()->view('pro/my_teams_SINGLE/SUB_dashboard', compact('record'), session('message') ? 500 : 200)->header('Content-Type', 'text/html');
+		$result_pros = DB::select("select id, name_display from pro");
+		return response()->view('pro/my_teams_SINGLE/SUB_dashboard', compact('record', 'result_pros'), session('message') ? 500 : 200)->header('Content-Type', 'text/html');
 	}
 	}
 
 
 	// GET /my_teams/view/{uid}/SUB_edit_demographics
 	// GET /my_teams/view/{uid}/SUB_edit_demographics

+ 3 - 0
composer.json

@@ -33,6 +33,9 @@
         }
         }
     },
     },
     "autoload": {
     "autoload": {
+        "files": [
+            "app/Helpers/Helper.php"
+        ],
         "psr-4": {
         "psr-4": {
             "App\\": "app/"
             "App\\": "app/"
         },
         },

+ 3 - 2
generatecv/tree.txt

@@ -31,8 +31,9 @@ PRO
             dashboard
             dashboard
                 !grp:Basic Details:team_number,created_at,client_count,is_active:Update Team Number:pencil-alt=>/my_teams/view/$uid/ACTION_updateTeamNumber
                 !grp:Basic Details:team_number,created_at,client_count,is_active:Update Team Number:pencil-alt=>/my_teams/view/$uid/ACTION_updateTeamNumber
                 !grp:Associated Pros:hcp_pro_id,ally_pro_id
                 !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
+                !qry:pros:select id, name_display from pro
+                !col:hcp_pro_id:HCP Pro:~pros:name_display:id,=,$hcp_pro_id:all
+                !col:ally_pro_id:Ally Pro:~pros:name_display:id,=,$ally_pro_id:all
                 !col:client_count:Clients:select '- TODO -' as result
                 !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
                 !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:team_number:Update Team Number:pencil-alt=>/my_teams/view/$uid/ACTION_updateTeamNumber

+ 2 - 4
resources/views/pro/my_teams_SINGLE/SUB_dashboard.blade.php

@@ -35,10 +35,8 @@ echo ($_r && count($_r)) ? $_r[0]->result : '-'; ?>@if( $record->is_active) <a u
         </tr>
         </tr>
         </thead>
         </thead>
         <tbody>
         <tbody>
-            <tr><td class="w-25 px-2 text-secondary border-right">HCP Pro</td><td class="w-75 px-2 font-weight-bold"><a href="/foo/bar/<?= $record->hcp_pro_id ?>"><?php $_r = \Illuminate\Support\Facades\DB::select("select name_display as result from pro where id = " . ($record->hcp_pro_id ? $record->hcp_pro_id : -1) . " limit 1");
-echo ($_r && count($_r)) ? $_r[0]->result : '-'; ?></a></td></tr>
-<tr><td class="w-25 px-2 text-secondary border-right">Ally Pro</td><td class="w-75 px-2 font-weight-bold"><?php $_r = \Illuminate\Support\Facades\DB::select("select name_display as result from pro where id = " . ($record->ally_pro_id ? $record->ally_pro_id : -1) . " limit 1");
-echo ($_r && count($_r)) ? $_r[0]->result : '-'; ?></td></tr>
+            <tr><td class="w-25 px-2 text-secondary border-right">HCP Pro</td><td class="w-75 px-2 font-weight-bold"><?= value_from_rs($result_pros, 'name_display', [['id', '=', $record->hcp_pro_id+1], ], 'all'); ?></td></tr>
+<tr><td class="w-25 px-2 text-secondary border-right">Ally Pro</td><td class="w-75 px-2 font-weight-bold"><?= value_from_rs($result_pros, 'name_display', [['id', '=', $record->ally_pro_id], ], 'all'); ?></td></tr>
         </tbody>
         </tbody>
     </table>
     </table>
 </div>
 </div>