|
@@ -66,11 +66,30 @@ class GenerateTreeCommand extends Command
|
|
|
$ls = $this->numLS($line);
|
|
|
$line = trim($line);
|
|
|
|
|
|
+ $exitURL = false;
|
|
|
+ if(strpos($line, "=>") !== FALSE) {
|
|
|
+ $parts = explode("=>", $line);
|
|
|
+ $exitURL = str_replace("UID", "{{ \$subRecord->uid }}", $parts[1]);
|
|
|
+ $line = $parts[0];
|
|
|
+ }
|
|
|
+
|
|
|
$tokens = explode("|", $line);
|
|
|
$line = $tokens[0];
|
|
|
$dbTable = null;
|
|
|
+ $condition = null;
|
|
|
if(count($tokens) >= 2) {
|
|
|
$dbTable = $tokens[1];
|
|
|
+
|
|
|
+ // check if table has loading conditions
|
|
|
+ if(strpos($dbTable, ":")) {
|
|
|
+ $parts = explode(":", $dbTable);
|
|
|
+ $dbTable = $parts[0];
|
|
|
+ $parts = explode("=", $parts[1]);
|
|
|
+ $condition = [
|
|
|
+ "field" => $parts[0],
|
|
|
+ "value" => str_replace("OWN", "session('proId')" , $parts[1])
|
|
|
+ ];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
$hasAdd = in_array("add", $tokens);
|
|
@@ -116,6 +135,7 @@ class GenerateTreeCommand extends Command
|
|
|
}
|
|
|
$currentController = new GenController($currentRoot, $line);
|
|
|
$currentController->dbTable = $dbTable;
|
|
|
+ $currentController->condition = $condition;
|
|
|
$currentController->hasAdd = $hasAdd;
|
|
|
$currentController->hasView = $hasView;
|
|
|
$currentController->hasRemove = $hasRemove;
|
|
@@ -149,12 +169,22 @@ class GenerateTreeCommand extends Command
|
|
|
break;
|
|
|
|
|
|
case 12: // ACTIONS | SUB
|
|
|
+ // check if this has show conditions
|
|
|
+ $show = false;
|
|
|
+ if(strpos($line, ":")) {
|
|
|
+ $parts = explode(":", $line);
|
|
|
+ $line = $parts[0];
|
|
|
+ if($parts[1] === 'if') {
|
|
|
+ $show = $parts[2];
|
|
|
+ }
|
|
|
+ }
|
|
|
if($currentSubType === 'ACTIONS') {
|
|
|
$currentMethod = $currentSubController->addMethod(
|
|
|
"ACTION_" . $line,
|
|
|
"/ACTION_" . $line
|
|
|
);
|
|
|
$currentMethod->type = 'action';
|
|
|
+ $currentMethod->show = $show;
|
|
|
$currentMethod->data = [];
|
|
|
}
|
|
|
else if($currentSubType === 'SUB') {
|
|
@@ -163,6 +193,7 @@ class GenerateTreeCommand extends Command
|
|
|
"/SUB_" . $line
|
|
|
);
|
|
|
$currentMethod->type = 'sub';
|
|
|
+ $currentMethod->show = $show;
|
|
|
$currentMethod->data = [];
|
|
|
}
|
|
|
break;
|
|
@@ -170,6 +201,14 @@ class GenerateTreeCommand extends Command
|
|
|
case 16: // data for actions and subs
|
|
|
if(!empty($currentMethod)) {
|
|
|
$currentMethod->data[] = $line;
|
|
|
+ if($exitURL) {
|
|
|
+ if(strpos("=", $line) !== FALSE) {
|
|
|
+ $currentMethod->viewURL = $exitURL;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $currentMethod->exitURL = $exitURL;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
break;
|
|
|
|
|
@@ -179,6 +218,11 @@ class GenerateTreeCommand extends Command
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
+ default:
|
|
|
+ dump("ERROR: Cannot have $ls leading spaces!");
|
|
|
+ dump("Line: $line");
|
|
|
+ exit(1);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -218,6 +262,7 @@ class GenController {
|
|
|
public $methods;
|
|
|
public $parentRoute = "";
|
|
|
public $dbTable = null;
|
|
|
+ public $condition = null;
|
|
|
public $hasAdd = false;
|
|
|
public $hasView = false;
|
|
|
public $hasRemove = false;
|
|
@@ -266,7 +311,13 @@ class GenController {
|
|
|
$newMethod->data[] = $method->data[$i];
|
|
|
}
|
|
|
$newMethod->parentSub = $this->name . '-' . $method->name;
|
|
|
- $newMethod->table = explode(":", $method->data[1])[1];
|
|
|
+ $parts = explode(":", $method->data[1]);
|
|
|
+ $newMethod->table = $parts[1];
|
|
|
+ if(count($parts) === 3) {
|
|
|
+ $newMethod->api = $parts[2];
|
|
|
+ }
|
|
|
+ $newMethod->exitURL = $method->exitURL;
|
|
|
+ $newMethod->showLink = false;
|
|
|
$newMethods[] = $newMethod;
|
|
|
$method->childAddRoute = $this->name . '-' . $methodName;
|
|
|
}
|
|
@@ -288,19 +339,47 @@ class GenController {
|
|
|
$input = ["'record'"];
|
|
|
// if sub-index controller, load subRecords
|
|
|
if($method->type === 'sub' && count($method->data)) {
|
|
|
- $dbParts = explode("=", $method->data[0]);
|
|
|
+ $parts = explode(",", $method->data[0]);
|
|
|
+
|
|
|
+ $loadingLine = [];
|
|
|
+
|
|
|
+ // first 'where'
|
|
|
+ $dbParts = explode("=", $parts[0]);
|
|
|
$localField = $dbParts[0];
|
|
|
$dbParts = explode(".", $dbParts[1]);
|
|
|
$foreignTable = $dbParts[0];
|
|
|
$foreignField = $dbParts[1];
|
|
|
- $code[] = "\t\t\$subRecords = DB::table('$foreignTable')->where('$foreignField', \$record->$localField)->get();";
|
|
|
+ $loadingLine[] = "\t\t\$subRecords = DB::table('$foreignTable')";
|
|
|
+ $loadingLine[] = "->where('$foreignField', \$record->$localField)";
|
|
|
+
|
|
|
+ // other 'where's
|
|
|
+ if(count($parts) > 1) {
|
|
|
+ for ($i = 1; $i < count($parts); $i++) {
|
|
|
+ $dbParts = explode("=", $parts[$i]);
|
|
|
+ $field = $dbParts[0];
|
|
|
+ $value = $dbParts[1];
|
|
|
+ $loadingLine[] = "->where('$field', $value)";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $loadingLine[] = "->get();";
|
|
|
+
|
|
|
+ $code[] = implode("", $loadingLine);
|
|
|
+
|
|
|
+ // $code[] = "\t\t\$subRecords = DB::table('$foreignTable')->where('$foreignField', \$record->$localField)->get();";
|
|
|
$input[] = "'subRecords'";
|
|
|
}
|
|
|
$code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', " .
|
|
|
"compact(" . implode(", ", $input) . "));";
|
|
|
}
|
|
|
else {
|
|
|
- $code[] = "\t\t\$records = DB::table('{$this->dbTable}')->get();";
|
|
|
+ $loadingLine = [];
|
|
|
+ $loadingLine[] = "\t\t\$records = DB::table('{$this->dbTable}')";
|
|
|
+ if($this->condition) {
|
|
|
+ $loadingLine[] = "->where('{$this->condition['field']}', {$this->condition['value']})";
|
|
|
+ }
|
|
|
+ $loadingLine[] = "->get();";
|
|
|
+ $code[] = implode("", $loadingLine);
|
|
|
$code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', " .
|
|
|
"compact('records'));";
|
|
|
}
|
|
@@ -410,9 +489,10 @@ class GenController {
|
|
|
$subLinksView = resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php");
|
|
|
$subLinks = [];
|
|
|
foreach ($controller->methods as $meth) {
|
|
|
- if (strpos($meth->name, "SUB_") !== 0) continue;
|
|
|
+ if (strpos($meth->name, "SUB_") !== 0 || $meth->showLink === false) continue;
|
|
|
$display = $this->snakeToTitleCase(substr($meth->name, 4));
|
|
|
- $subLinks[] = "<a " .
|
|
|
+ $subLinks[] = ($meth->show ? "@if(\$record->{$meth->show}) " : "") .
|
|
|
+ "<a " .
|
|
|
"href='/{$controller->parentControllerName}/view/<?= \$record->uid ?>/{$meth->name}' " .
|
|
|
"class='d-block px-3 py-2 border-bottom " .
|
|
|
"{{ request()->route()->getActionMethod() === '{$meth->name}' ? 'bg-secondary text-white font-weight-bold' : '' }}" .
|
|
@@ -421,7 +501,8 @@ class GenController {
|
|
|
"{{ strpos(request()->route()->getActionMethod(), 'ACTION_') === 0 ? 'bg-secondary text-white font-weight-bold' : '' }}" :
|
|
|
""
|
|
|
)
|
|
|
- . "'>$display</a>";
|
|
|
+ . "'>$display</a>" .
|
|
|
+ ($meth->show ? " @endif" : "");
|
|
|
}
|
|
|
$this->file_force_contents($subLinksView, implode("\n", $subLinks));
|
|
|
echo "Generated " . $subLinksView . "\n";
|
|
@@ -433,11 +514,13 @@ class GenController {
|
|
|
$actionLinksView = resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php");
|
|
|
$actionLinks = [];
|
|
|
foreach ($controller->methods as $meth) {
|
|
|
- if (strpos($meth->name, "ACTION_") !== 0) continue;
|
|
|
+ if (strpos($meth->name, "ACTION_") !== 0 || $meth->showLink === false) continue;
|
|
|
$display = $this->camelToTitleCase(substr($meth->name, 7));
|
|
|
- $actionLinks[] = "<a " .
|
|
|
+ $actionLinks[] = ($meth->show ? "@if(\$record->{$meth->show}) " : "") .
|
|
|
+ "<a " .
|
|
|
"href='/{$controller->parentControllerName}/view/<?= \$record->uid ?>/{$meth->name}' " .
|
|
|
- "class='d-block btn btn-sm btn-default mb-3'>$display</a>";
|
|
|
+ "class='d-block btn btn-sm btn-default mb-3'>$display</a>" .
|
|
|
+ ($meth->show ? " @endif" : "");
|
|
|
}
|
|
|
$this->file_force_contents($actionLinksView, implode("\n", $actionLinks));
|
|
|
echo "Generated " . $actionLinksView . "\n";
|
|
@@ -467,7 +550,7 @@ class GenController {
|
|
|
$text = str_replace("_API_", "/api/{$this->snakeToCamelCase($controller->dbTable)}/" . substr($method->name, 7), $text);
|
|
|
}
|
|
|
else {
|
|
|
- $text = str_replace("_API_", "/api/{$this->snakeToCamelCase($method->table)}/create", $text);
|
|
|
+ $text = str_replace("_API_", "/api/{$this->snakeToCamelCase($method->table)}/{$method->api}", $text);
|
|
|
}
|
|
|
|
|
|
$text = str_replace("_RETURN_ROUTE_", "{$controller->name}-{$method->name}", $text);
|
|
@@ -502,7 +585,11 @@ class GenController {
|
|
|
$tds = [];
|
|
|
foreach ($columns as $column) {
|
|
|
$ths[] = "<th>{$this->snakeToTitleCase($column)}</th>";
|
|
|
- $tds[] = "<td><?= \$subRecord->$column ?></td>";
|
|
|
+ $tds[] = "<td>" .
|
|
|
+ ($method->exitURL && $column === 'uid' ? '<a href="' . $method->exitURL . '">' : '') .
|
|
|
+ "<?= \$subRecord->$column ?>" .
|
|
|
+ ($method->exitURL && $column === 'uid' ? '</a>' : '') .
|
|
|
+ "</td>";
|
|
|
}
|
|
|
$text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
|
|
|
$text = str_replace("<!-- __SCAFFOLD_TDS__ -->", implode("\n", $tds), $text);
|
|
@@ -563,6 +650,8 @@ class GenController {
|
|
|
$this->w('Rout: ' . $method->route, 1);
|
|
|
$this->w('Meth: ' . $method->name . '($request' . ($method->hasUID ? ', $uid' : '') . ')', 1);
|
|
|
if(!empty($method->data)) $this->w('Data: ' . implode(", ", $method->data), 1);
|
|
|
+ $this->w('Exit: ' . $method->exitURL, 1);
|
|
|
+ $this->w('View: ' . $method->viewURL, 1);
|
|
|
if(!$method->redirect) {
|
|
|
$this->w('View: ' . resource_path("views/{$this->root}/{$this->name}/{$method->name}.blade.php"), 1);
|
|
|
}
|
|
@@ -598,7 +687,7 @@ class GenController {
|
|
|
$file = array_pop($parts);
|
|
|
$dir = '';
|
|
|
foreach($parts as $part) {
|
|
|
- if($part[strlen($part) - 1] !== ':') {
|
|
|
+ if(strlen($part) === 0 || $part[strlen($part) - 1] !== ':') {
|
|
|
if(!is_dir($dir .= "/$part")) mkdir($dir);
|
|
|
}
|
|
|
}
|
|
@@ -634,7 +723,7 @@ class GenController {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if($type !== 'hidden') {
|
|
|
+ if($type !== 'hidden' && $type !== 'bool') {
|
|
|
$code[] = "<div class='form-group mb-3'>";
|
|
|
$code[] = "<label class='control-label'>{$this->camelToTitleCase($this->snakeToTitleCase($display))}</label>";
|
|
|
}
|
|
@@ -663,9 +752,14 @@ class GenController {
|
|
|
$code[] = "<?php endforeach; ?>";
|
|
|
$code[] = "</select>";
|
|
|
break;
|
|
|
+ case "bool":
|
|
|
+ $code[] = "<div class='form-group mb-3'>";
|
|
|
+ $code[] = "<label class='control-label'>{$this->camelToTitleCase($this->snakeToTitleCase($display))} ";
|
|
|
+ $code[] = "<input class='form-control' type='checkbox' name='$name'>";
|
|
|
+ $code[] = "</label>";
|
|
|
+ break;
|
|
|
default:
|
|
|
- $code[] = "<input class='form-control' type='$type' name='$name' " . $valueLine .
|
|
|
- ">";
|
|
|
+ $code[] = "<input class='form-control' type='$type' name='$name' " . $valueLine . ">";
|
|
|
}
|
|
|
if($type !== 'hidden') {
|
|
|
$code[] = "</div>";
|
|
@@ -685,6 +779,10 @@ class GenControllerMethod {
|
|
|
public $childAddRoute = false;
|
|
|
public $table = false;
|
|
|
public $api = 'create';
|
|
|
+ public $show = null;
|
|
|
+ public $viewURL = false;
|
|
|
+ public $exitURL = false;
|
|
|
+ public $showLink = true;
|
|
|
public function __construct($name, $route)
|
|
|
{
|
|
|
$this->name = $name;
|