GenerateTreeCommand.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. class GenerateTreeCommand extends Command
  6. {
  7. private $routesFile = null;
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'generatetree
  14. {path: /path/to/tree.txt}';
  15. /**
  16. * Execute the console command.
  17. *
  18. * @return mixed
  19. */
  20. public function handle()
  21. {
  22. $lines = ['<?php', '', 'use Illuminate\Support\Facades\Route;', '', ''];
  23. file_put_contents(base_path("routes/generated.php"), implode("\n", $lines));
  24. $sideLinks = [];
  25. global $argv;
  26. $file = fopen($argv[2], "r");
  27. $lines = [];
  28. while (!feof($file)) {
  29. $line = rtrim(fgets($file));
  30. if(trim($line) !== '') {
  31. $lines[] = str_replace("\t", " ", $line);
  32. }
  33. }
  34. fclose($file);
  35. $currentRoot = "";
  36. $currentController = new GenController();
  37. $currentSubController = new GenController();
  38. $currentSubType = "";
  39. $currentView = "";
  40. $currentMethod = null;
  41. foreach ($lines as $line) {
  42. // skip comments
  43. if(trim($line)[0] === '#') continue;
  44. $lineType = null;
  45. // no leading space - root specifier
  46. if($line[0] !== ' ') {
  47. $currentRoot = strtolower(trim($line));
  48. }
  49. else {
  50. $ls = $this->numLS($line);
  51. $line = trim($line);
  52. $exitURL = false;
  53. if(strpos($line, "=>") !== FALSE) {
  54. $parts = explode("=>", $line);
  55. $exitURL = str_replace("UID", "{{ \$subRecord->uid }}", $parts[1]);
  56. $line = $parts[0];
  57. }
  58. $tokens = explode("|", $line);
  59. $line = $tokens[0];
  60. $dbTable = null;
  61. $condition = null;
  62. if(count($tokens) >= 2) {
  63. $dbTable = $tokens[1];
  64. // check if table has loading conditions
  65. if(strpos($dbTable, ":")) {
  66. $parts = explode(":", $dbTable);
  67. $dbTable = $parts[0];
  68. $parts = explode("=", $parts[1]);
  69. $condition = [
  70. "field" => $parts[0],
  71. "value" => str_replace("OWN", "session('proId')" , $parts[1])
  72. ];
  73. }
  74. }
  75. $hasAdd = in_array("add", $tokens);
  76. $hasView = in_array("view", $tokens);
  77. $hasRemove = in_array("remove", $tokens);
  78. $icon = "user";
  79. if(strpos($tokens[count($tokens) - 1], "icon:") === 0) {
  80. $icon = substr($tokens[count($tokens) - 1], 5);
  81. }
  82. switch($ls) {
  83. case 4: // top level controller OR top level controller action
  84. // top level controller-action
  85. if(strpos($line, "/") !== FALSE) {
  86. if(!empty($currentController)) {
  87. $parts = explode(":", $line);
  88. $line = $parts[0];
  89. $method = explode("/", $line)[1];
  90. $newMethod = $currentController->addMethod($method, "/" . $line);
  91. if(count($parts) > 1) {
  92. $newMethod->api = $parts[count($parts) - 1];
  93. }
  94. // create _SINGLE_ controller if view
  95. if($method === "view") {
  96. $currentSubController = new GenController($currentRoot, $currentController->name . "_SINGLE");
  97. $currentSubController->dbTable = $currentController->dbTable;
  98. $currentSubController->parentRoute = "/" . $line;
  99. $currentSubController->parentControllerName = $currentController->name;
  100. $currentSubController->sub = true;
  101. $newMethod->redirect = "/" . $line . "/SUB_dashboard";
  102. }
  103. else if(strpos($method, "add_new") === 0) {
  104. $newMethod->type = 'add';
  105. }
  106. else if(strpos($method, "remove") === 0) {
  107. $newMethod->type = 'remove';
  108. }
  109. $currentMethod = $newMethod;
  110. }
  111. }
  112. // new top level controller
  113. else if(empty($currentController) || $line !== $currentController->name) {
  114. if(!empty($currentController)) {
  115. $currentController->save();
  116. }
  117. $currentController = new GenController($currentRoot, $line, $icon);
  118. $currentController->dbTable = $dbTable;
  119. $currentController->condition = $condition;
  120. $currentController->hasAdd = $hasAdd;
  121. $currentController->hasView = $hasView;
  122. $currentController->hasRemove = $hasRemove;
  123. $currentMethod = $currentController->addMethod("index", "/$line");
  124. if(!empty($currentSubController)) {
  125. $currentSubController->save();
  126. }
  127. $currentSubType = '';
  128. $currentSubController = new GenController();
  129. $sideLinks[] = "<li class='nav-item'><a href='/{$currentController->name}' " .
  130. "class='nav-link " .
  131. "{{ (isset(request()->route()->getController()->selfName) && strpos(request()->route()->getController()->selfName, '{$currentController->name}') === 0 ? 'active' : '') }}" . " '>" .
  132. "<i class='nav-icon fa fa-$icon'></i>" .
  133. "<p>" . $currentController->snakeToTitleCase($currentController->name) . "</p>" .
  134. "</a></li>";
  135. }
  136. break;
  137. case 8: // sub-type declaration | add_new fields | !inc/!exc
  138. if($line === 'ACTIONS' || $line === 'SUB') {
  139. $currentSubType = $line;
  140. }
  141. else if(strpos($line, "!inc:") === 0) { // !inc:
  142. if (!empty($currentMethod)) {
  143. $currentMethod->setIncFields('include', explode(",", substr($line, 5)));
  144. }
  145. }
  146. else if(strpos($line, "!exc:") === 0) { // !exc:
  147. if (!empty($currentMethod)) {
  148. $currentMethod->setIncFields('exclude', explode(",", substr($line, 5)));
  149. }
  150. }
  151. else if(strpos($line, "!lnk:") === 0) { // !lnk:
  152. if (!empty($currentMethod)) {
  153. $currentMethod->viewLinkField = substr($line, 5);
  154. }
  155. }
  156. else if(strpos($line, "!col:") === 0) { // !col:
  157. if (!empty($currentMethod)) {
  158. $currentMethod->setColumnSpec(substr($line, 5), $exitURL, 'record');
  159. }
  160. }
  161. else if (!empty($currentMethod) &&
  162. (strpos($currentMethod->name, 'add_new') === 0 ||
  163. $currentMethod->name === 'remove')) { // this is a field in add_new
  164. $currentMethod->data[] = $line;
  165. }
  166. break;
  167. case 12: // ACTIONS | SUB
  168. // check if this has show conditions
  169. $show = false;
  170. if(strpos($line, ":")) {
  171. $parts = explode(":", $line);
  172. $line = $parts[0];
  173. if($parts[1] === 'if') {
  174. $show = $parts[2];
  175. }
  176. }
  177. if($currentSubType === 'ACTIONS') {
  178. $currentMethod = $currentSubController->addMethod(
  179. "ACTION_" . $line,
  180. "/ACTION_" . $line
  181. );
  182. $currentMethod->type = 'action';
  183. $currentMethod->show = $show;
  184. $currentMethod->data = [];
  185. }
  186. else if($currentSubType === 'SUB') {
  187. $currentMethod = $currentSubController->addMethod(
  188. "SUB_" . $line,
  189. "/SUB_" . $line
  190. );
  191. $currentMethod->type = 'sub';
  192. $currentMethod->show = $show;
  193. $currentMethod->data = [];
  194. }
  195. break;
  196. case 16: // data for actions and subs
  197. if(strpos($line, "!inc:") === 0) { // !inc:
  198. if (!empty($currentMethod)) {
  199. $currentMethod->setIncFields('include', explode(",", substr($line, 5)));
  200. }
  201. }
  202. else if(strpos($line, "!exc:") === 0) { // !exc:
  203. if (!empty($currentMethod)) {
  204. $currentMethod->setIncFields('exclude', explode(",", substr($line, 5)));
  205. }
  206. }
  207. else if(strpos($line, "!lnk:") === 0) { // !lnk:
  208. if (!empty($currentMethod)) {
  209. $currentMethod->viewLinkField = substr($line, 5);
  210. }
  211. }
  212. else if(strpos($line, "!col:") === 0) { // !col:
  213. if (!empty($currentMethod)) {
  214. $currentMethod->setColumnSpec(substr($line, 5), $exitURL,
  215. $currentMethod->dashboard ? 'record' : 'subRecord'
  216. );
  217. }
  218. }
  219. else if(strpos($line, "!grp:") === 0) { // !grp:
  220. if (!empty($currentMethod)) {
  221. $currentMethod->addGroup(substr($line, 5), $exitURL);
  222. }
  223. }
  224. else if(strpos($line, "!act:") === 0) { // !act:
  225. if (!empty($currentMethod)) {
  226. $currentMethod->addAction(substr($line, 5), $exitURL);
  227. }
  228. }
  229. else if(strpos($line, "!nal:") === 0) { // !nal:
  230. if (!empty($currentMethod)) {
  231. $currentMethod->noActionLinks = true;
  232. }
  233. }
  234. else if(!empty($currentMethod)) {
  235. $currentMethod->data[] = $line;
  236. if($exitURL) {
  237. if(strpos("=", $line) !== FALSE) {
  238. $currentMethod->viewURL = $exitURL;
  239. }
  240. else {
  241. $currentMethod->exitURL = $exitURL;
  242. }
  243. }
  244. }
  245. break;
  246. case 20: // SUB add_new fields
  247. if(!empty($currentMethod)) {
  248. $currentMethod->data[] = $line;
  249. }
  250. break;
  251. default:
  252. dump("ERROR: Cannot have $ls leading spaces!");
  253. dump("Line: $line");
  254. exit(1);
  255. }
  256. }
  257. }
  258. // do any pending saves
  259. if(!empty($currentSubController)) {
  260. $currentSubController->save();
  261. }
  262. if(!empty($currentController)) {
  263. $currentController->save();
  264. }
  265. echo "Saved " . base_path("routes/generated.php") . "\n";
  266. // save side links
  267. file_put_contents(resource_path("views/layouts/generated-links.blade.php"), implode("\n", $sideLinks));
  268. echo "Saved " . resource_path("views/layouts/generated-links.blade.php") . "\n";
  269. }
  270. private function numLS($line) {
  271. $count = 0;
  272. for ($i=0; $i<strlen($line); $i++) {
  273. if($line[$i] !== ' ') break;
  274. $count++;
  275. }
  276. return $count;
  277. }
  278. }
  279. class GenController {
  280. public $root;
  281. public $saved = false;
  282. public $name;
  283. public $methods;
  284. public $parentRoute = "";
  285. public $dbTable = null;
  286. public $condition = null;
  287. public $hasAdd = false;
  288. public $hasView = false;
  289. public $hasRemove = false;
  290. public $sub = false;
  291. public $parentControllerName = '';
  292. public $subLinksSaved = false;
  293. public $actionLinksSaved = false;
  294. public $icon = "user";
  295. public function __construct($root = null, $name = null, $icon = "user")
  296. {
  297. $this->root = $root;
  298. $this->name = $name;
  299. $this->icon = $icon;
  300. $this->methods = [];
  301. }
  302. public function addMethod($method, $route) {
  303. if($this->parentRoute) {
  304. $route = $this->parentRoute . $route;
  305. }
  306. $method = new GenControllerMethod($method, $route);
  307. $this->methods[] = $method;
  308. return $method;
  309. }
  310. public function save() {
  311. if(!$this->saved && !empty($this->root) && !empty($this->name)) {
  312. $this->saveController();
  313. $this->saveRoutes();
  314. $this->saved = true;
  315. // $this->log();
  316. }
  317. }
  318. public function saveController() {
  319. $text = file_get_contents(base_path('generatecv/tree-templates/controller.template.php'));
  320. $code = [];
  321. // check if any method has a "sub add_new" in it, if yes, add action for the same
  322. $newMethods = [];
  323. foreach ($this->methods as $method) {
  324. if($method->type === 'sub' && count($method->data) > 1 && strpos($method->data[1], 'add_new') === 0) {
  325. $methodName = preg_replace("/^SUB_/", "ACTION_", $method->name) . 'AddNew';
  326. $methodRoute = str_replace("/SUB_", "/ACTION_", $method->route) . 'AddNew';
  327. $newMethod = new GenControllerMethod($methodName, $methodRoute);
  328. $newMethod->hasUID = true;
  329. $newMethod->redirect = false;
  330. $newMethod->type = 'action';
  331. $newMethod->data = [];
  332. for($i = 2; $i<count($method->data); $i++) {
  333. $newMethod->data[] = $method->data[$i];
  334. }
  335. $newMethod->parentSub = $this->name . '-' . $method->name;
  336. $parts = explode(":", $method->data[1]);
  337. $newMethod->table = $parts[1];
  338. if(count($parts) === 3) {
  339. $newMethod->api = $parts[2];
  340. }
  341. $newMethod->exitURL = $method->exitURL;
  342. $newMethod->showLink = false;
  343. $newMethods[] = $newMethod;
  344. $method->childAddRoute = $this->name . '-' . $methodName;
  345. }
  346. }
  347. $this->methods = array_merge($this->methods, $newMethods);
  348. foreach ($this->methods as $method) {
  349. $code[] = "";
  350. $code[] = "\t// GET {$method->route}";
  351. $code[] = "\t" . 'public function ' . $method->name . '(Request $request' . ($method->hasUID ? ', $uid' : '') . ') {';
  352. if($method->redirect) {
  353. $target = str_replace('{uid}', '$uid', $method->redirect);
  354. $code[] = "\t\t" . 'return redirect("' . $target . '");';
  355. }
  356. else {
  357. if($method->hasUID) {
  358. $code[] = "\t\t\$record = DB::table('{$this->dbTable}')->where('uid', \$uid)->first();";
  359. $input = ["'record'"];
  360. // if sub-index controller, load subRecords
  361. if($method->type === 'sub' && count($method->data)) {
  362. $parts = explode(",", $method->data[0]);
  363. $loadingLine = [];
  364. // first 'where'
  365. $dbParts = explode("=", $parts[0]);
  366. $localField = $dbParts[0];
  367. $dbParts = explode(".", $dbParts[1]);
  368. $foreignTable = $dbParts[0];
  369. $foreignField = $dbParts[1];
  370. $loadingLine[] = "\t\t\$subRecords = DB::table('$foreignTable')";
  371. $loadingLine[] = "->where('$foreignField', \$record->$localField)";
  372. // other 'where's
  373. if(count($parts) > 1) {
  374. for ($i = 1; $i < count($parts); $i++) {
  375. $dbParts = explode("=", $parts[$i]);
  376. $field = $dbParts[0];
  377. $value = $dbParts[1];
  378. $loadingLine[] = "->where('$field', $value)";
  379. }
  380. }
  381. $loadingLine[] = "->get();";
  382. $code[] = implode("", $loadingLine);
  383. // $code[] = "\t\t\$subRecords = DB::table('$foreignTable')->where('$foreignField', \$record->$localField)->get();";
  384. $input[] = "'subRecords'";
  385. }
  386. // return response()->view('pro/my_teams/add_new', compact('records'), session('message') ? 500 : 200)->header('Content-Type', 'text/html');
  387. $code[] = "\t\treturn response()->view('{$this->root}/{$this->name}/{$method->name}', " .
  388. "compact(" . implode(", ", $input) . "), " .
  389. "session('message') ? 500 : 200)->header('Content-Type', 'text/html');";
  390. }
  391. else {
  392. $loadingLine = [];
  393. $loadingLine[] = "\t\t\$records = DB::table('{$this->dbTable}')";
  394. if($this->condition) {
  395. $loadingLine[] = "->where('{$this->condition['field']}', {$this->condition['value']})";
  396. }
  397. $loadingLine[] = "->get();";
  398. $code[] = implode("", $loadingLine);
  399. $code[] = "\t\treturn response()->view('{$this->root}/{$this->name}/{$method->name}', " .
  400. "compact('records'), " .
  401. "session('message') ? 500 : 200)->header('Content-Type', 'text/html');";
  402. }
  403. }
  404. $this->saveView($this, $method);
  405. $code[] = "\t}";
  406. }
  407. $text = str_replace("_NAME_", "{$this->name}_Controller", $text);
  408. $text = str_replace("// __METHODS__", implode("\n", $code), $text);
  409. file_put_contents(app_path("Http/Controllers/{$this->name}_Controller.php"), $text);
  410. echo "Generated " . app_path("Http/Controllers/{$this->name}_Controller.php") . "\n";
  411. }
  412. public function saveView(GenController $controller, GenControllerMethod $method) {
  413. if($controller->sub) {
  414. $controller->saveSubLinks($controller, $method);
  415. $controller->saveActionLinks($controller, $method);
  416. if($method->type === 'action') {
  417. $this->saveSubActionView($controller, $method);
  418. }
  419. else if($method->type === 'sub' && count($method->data)) {
  420. $this->saveSubIndexView($controller, $method);
  421. }
  422. else {
  423. $this->saveSubDefaultView($controller, $method);
  424. }
  425. }
  426. else {
  427. if($method->name === 'view' && $controller->hasView) {
  428. $this->saveShowView($controller, $method);
  429. }
  430. else if($method->name === 'index') {
  431. $this->saveIndexView($controller, $method);
  432. }
  433. else if(strpos($method->name, 'add_new') === 0 && $controller->hasAdd) {
  434. $this->saveAddNewView($controller, $method);
  435. }
  436. else if($method->name === 'remove' && $controller->hasRemove) {
  437. $this->saveAddNewView($controller, $method);
  438. }
  439. }
  440. }
  441. public function saveIndexView(GenController $controller, GenControllerMethod $method)
  442. {
  443. $text = file_get_contents(base_path('generatecv/tree-templates/index.template.blade.php'));
  444. $text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
  445. if($controller->hasAdd) {
  446. $addLinks = [];
  447. foreach ($controller->methods as $m) {
  448. if($m->type === 'add') {
  449. $addLinks[] = "<a class='btn btn-primary btn-sm ml-2' " .
  450. 'up-modal=".form-contents" up-width="800" up-history="false" ' .
  451. "href='/{$controller->name}/{$m->name}'>" .
  452. "<i class='fa fa-plus-circle' aria-hidden='true'></i> " .
  453. "{$this->snakeToTitleCase($m->name)}</a>";
  454. }
  455. }
  456. $text = str_replace("<!-- _ADD_NEW_LINK_ -->", implode("\n", $addLinks), $text);
  457. }
  458. $columns = DB::getSchemaBuilder()->getColumnListing($controller->dbTable);
  459. $ths = [];
  460. $tds = [];
  461. if($controller->hasRemove) {
  462. $ths[] = "<th></th>";
  463. $tds[] = "<td><a href='/{$controller->name}/remove/<?= \$record->uid ?>'>" .
  464. "<i class='fa fa-trash'></i>" .
  465. "</a></td>";
  466. }
  467. // !inc/!exc
  468. if($method->incType === "include") {
  469. $columns = $method->incFields;
  470. }
  471. else if($method->incType === "exclude") {
  472. $columns = array_filter($columns, function($item) use ($method) {
  473. return in_array($item, $method->incFields) === FALSE;
  474. });
  475. }
  476. foreach ($columns as $column) {
  477. $columnTitle = $this->snakeToTitleCase($column);
  478. $columnValue = "<?= \$record->$column ?>";
  479. $hasLink = $controller->hasView && $column === $method->viewLinkField;
  480. $linkTarget = "/{$controller->name}/view/<?= \$record->uid ?>";
  481. // check if this column has column spec
  482. if(isset($method->columns[$column])) {
  483. $columnTitle = $method->columns[$column]["label"];
  484. if(isset($method->columns[$column]["query"])) {
  485. $columnValue = "<?php \$_r = \Illuminate\Support\Facades\DB::" .
  486. "select(\"{$method->columns[$column]["query"]}\");\n" .
  487. "echo (\$_r && count(\$_r)) ? \$_r[0]->result : '-'; ?>";
  488. }
  489. if(isset($method->columns[$column]["link"])) {
  490. $hasLink = true;
  491. $linkTarget = $method->columns[$column]["link"];
  492. }
  493. }
  494. $ths[] = "<th>$columnTitle</th>";
  495. $tds[] = "<td>" .
  496. ($hasLink ? "<a href=\"{$linkTarget}\">" : "") .
  497. $columnValue .
  498. ($hasLink ? "</a>" : "") .
  499. "</td>";
  500. }
  501. $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
  502. $text = str_replace("<!-- __SCAFFOLD_TDS__ -->", implode("\n", $tds), $text);
  503. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  504. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  505. }
  506. public function saveShowView(GenController $controller, GenControllerMethod $method)
  507. {
  508. // delete sub links and action links
  509. if(file_exists(resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php"))) {
  510. unlink(resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php"));
  511. }
  512. if(file_exists(resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php"))) {
  513. unlink(resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php"));
  514. }
  515. // write info view
  516. $text = file_get_contents(base_path('generatecv/tree-templates/info.template.blade.php'));
  517. $text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
  518. $text = str_replace("_UID_", '<?= $record->uid ?>', $text);
  519. $text = str_replace("_INDEX_ROUTE_", $controller->name . '-index', $text);
  520. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/info.blade.php"), $text);
  521. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/info.blade.php") . "\n";
  522. // write main view
  523. $text = file_get_contents(base_path('generatecv/tree-templates/show.template.blade.php'));
  524. $text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
  525. $text = str_replace("_UID_", '<?= $record->uid ?>', $text);
  526. $text = str_replace("_INDEX_ROUTE_", $controller->name . '-index', $text);
  527. $text = str_replace("_SUB_LINKS_VIEW_", "{$controller->root}/{$controller->name}/subs", $text);
  528. $text = str_replace("_INFO_VIEW_", "{$controller->root}/{$controller->name}/info", $text);
  529. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  530. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  531. }
  532. public function saveSubLinks(GenController $controller, GenControllerMethod $method)
  533. {
  534. if ($controller->subLinksSaved) return;
  535. $subLinksView = resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php");
  536. $subLinks = [];
  537. foreach ($controller->methods as $meth) {
  538. if (strpos($meth->name, "SUB_") !== 0 || $meth->showLink === false) continue;
  539. $display = $this->snakeToTitleCase(substr($meth->name, 4));
  540. $subLinks[] = ($meth->show ? "@if(\$record->{$meth->show}) " : "") .
  541. "<a " .
  542. "href='/{$controller->parentControllerName}/view/<?= \$record->uid ?>/{$meth->name}' " .
  543. "class='d-block px-3 py-2 border-bottom " .
  544. "{{ request()->route()->getActionMethod() === '{$meth->name}' ? 'bg-secondary text-white font-weight-bold' : '' }}" .
  545. (
  546. $meth->name === 'SUB_dashboard' ?
  547. "{{ strpos(request()->route()->getActionMethod(), 'ACTION_') === 0 ? 'bg-secondary text-white font-weight-bold' : '' }}" :
  548. ""
  549. )
  550. . "'>$display</a>" .
  551. ($meth->show ? " @endif" : "");
  552. }
  553. $this->file_force_contents($subLinksView, implode("\n", $subLinks));
  554. echo "Generated " . $subLinksView . "\n";
  555. $controller->subLinksSaved = true;
  556. }
  557. public function saveActionLinks(GenController $controller, GenControllerMethod $method)
  558. {
  559. if ($controller->actionLinksSaved) return;
  560. $actionLinksView = resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php");
  561. $actionLinks = [];
  562. foreach ($controller->methods as $meth) {
  563. if (strpos($meth->name, "ACTION_") !== 0 || $meth->showLink === false) continue;
  564. $display = $this->camelToTitleCase(substr($meth->name, 7));
  565. $actionLinks[] = ($meth->show ? "@if(\$record->{$meth->show}) " : "") .
  566. "<a " .
  567. 'up-modal=".form-contents" up-width="800" up-history="false" ' .
  568. "href='/{$controller->parentControllerName}/view/<?= \$record->uid ?>/{$meth->name}' " .
  569. "class='d-block btn btn-sm btn-default mb-3'>$display</a>" .
  570. ($meth->show ? " @endif" : "");
  571. }
  572. $this->file_force_contents($actionLinksView, implode("\n", $actionLinks));
  573. echo "Generated " . $actionLinksView . "\n";
  574. $controller->actionLinksSaved = true;
  575. }
  576. public function saveSubDefaultView(GenController $controller, GenControllerMethod $method)
  577. {
  578. $text = file_get_contents(base_path('generatecv/tree-templates/sub.template.blade.php'));
  579. $text = str_replace("_LAYOUT_", "{$controller->root}.{$controller->parentControllerName}.view", $text);
  580. $text = $this->generateSubContent($controller, $method, $text);
  581. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  582. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  583. }
  584. public function saveSubActionView(GenController $controller, GenControllerMethod $method) {
  585. $text = file_get_contents(base_path('generatecv/tree-templates/sub-action.template.blade.php'));
  586. $text = str_replace("_LAYOUT_", "{$controller->root}.{$controller->parentControllerName}.view", $text);
  587. $text = str_replace("_NAME_", $this->camelToTitleCase($this->snakeToTitleCase($method->name)), $text);
  588. if(!$method->parentSub) {
  589. $text = str_replace("_BACK_ROUTE_", "{$controller->parentControllerName}-view", $text);
  590. }
  591. else {
  592. $text = str_replace("_BACK_ROUTE_", $method->parentSub, $text);
  593. }
  594. if(!$method->table) {
  595. $text = str_replace("_API_", "/api/{$this->snakeToCamelCase($controller->dbTable)}/" . substr($method->name, 7), $text);
  596. }
  597. else {
  598. $text = str_replace("_API_", "/api/{$this->snakeToCamelCase($method->table)}/{$method->api}", $text);
  599. }
  600. $text = str_replace("_RETURN_ROUTE_", "{$controller->name}-{$method->name}", $text);
  601. $fields = [];
  602. if(count($method->data)) {
  603. foreach ($method->data as $field) {
  604. $fields[] = $this->generateFormField($field);
  605. }
  606. }
  607. $text = str_replace("<!-- _SCAFFOLD_FIELDS_ -->", implode("\n", $fields), $text);
  608. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  609. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  610. }
  611. public function saveSubIndexView(GenController $controller, GenControllerMethod $method) {
  612. $text = file_get_contents(base_path('generatecv/tree-templates/sub-index.template.blade.php'));
  613. $text = str_replace("_LAYOUT_", "{$controller->root}.{$controller->parentControllerName}.view", $text);
  614. $text = str_replace("_NAME_", $this->camelToTitleCase($this->snakeToTitleCase($method->name)), $text);
  615. if(count($method->data) > 1 && strpos($method->data[1], 'add_new') === 0) {
  616. $addLink = '<a class="btn btn-primary btn-sm" ' .
  617. 'up-modal=".form-contents" up-width="800" up-history="false" ' .
  618. 'href="{{route(\'' . $method->childAddRoute . '\', [\'uid\' => $record->uid])}}">' .
  619. "<i class='fa fa-plus-circle' aria-hidden='true'></i> Add New</a>";
  620. $text = str_replace("<!-- _ADD_NEW_LINK_ -->", $addLink, $text);
  621. }
  622. $dbParts = explode("=", $method->data[0]);
  623. $dbParts = explode(".", $dbParts[1]);
  624. $table = $dbParts[0];
  625. $columns = DB::getSchemaBuilder()->getColumnListing($table);
  626. $ths = [];
  627. $tds = [];
  628. // !inc/!exc
  629. if($method->incType === "include") {
  630. $columns = $method->incFields;
  631. }
  632. else if($method->incType === "exclude") {
  633. $columns = array_filter($columns, function($item) use ($method) {
  634. return in_array($item, $method->incFields) === FALSE;
  635. });
  636. }
  637. foreach ($columns as $column) {
  638. $columnTitle = $this->snakeToTitleCase($column);
  639. $columnValue = "<?= \$subRecord->$column ?>";
  640. $hasLink = $method->exitURL && $column === $method->viewLinkField;
  641. $linkTarget = $method->exitURL;
  642. // check if this column has column spec
  643. if(isset($method->columns[$column])) {
  644. $columnTitle = $method->columns[$column]["label"];
  645. if(isset($method->columns[$column]["query"])) {
  646. $columnValue = "<?php \$_r = \Illuminate\Support\Facades\DB::" .
  647. "select(\"{$method->columns[$column]["query"]}\");\n" .
  648. "echo (\$_r && count(\$_r)) ? \$_r[0]->result : '-'; ?>";
  649. }
  650. if(isset($method->columns[$column]["link"])) {
  651. $hasLink = true;
  652. $linkTarget = $method->columns[$column]["link"];
  653. }
  654. }
  655. $ths[] = "<th>$columnTitle</th>";
  656. $tds[] = "<td>" .
  657. ($hasLink ? "<a href=\"{$linkTarget}\">" : "") .
  658. $columnValue .
  659. ($hasLink ? "</a>" : "") .
  660. "</td>";
  661. }
  662. $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
  663. $text = str_replace("<!-- __SCAFFOLD_TDS__ -->", implode("\n", $tds), $text);
  664. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  665. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  666. }
  667. public function generateSubContent(GenController $controller, GenControllerMethod $method, $text) {
  668. if($method->dashboard) {
  669. if(!isset($method->groups) || !count($method->groups)) {
  670. $html = file_get_contents(base_path('generatecv/tree-templates/dashboard' . ($method->noActionLinks ? '-nal' : '') . '.template.blade.php'));
  671. }
  672. else {
  673. $html = file_get_contents(base_path('generatecv/tree-templates/dashboard-grouped' . ($method->noActionLinks ? '-nal' : '') . '.template.blade.php'));
  674. $groupsHtml = [];
  675. $groupTemplate = file_get_contents(base_path('generatecv/tree-templates/dashboard-group.template.blade.php'));
  676. foreach ($method->groups as $group) {
  677. $groupHtml = $groupTemplate;
  678. $groupHtml = str_replace("<!-- __GROUP_NAME__ -->", $group["name"], $groupHtml);
  679. if (isset($group["action"])) {
  680. $action = $group["action"];
  681. $actionLine = [];
  682. if (isset($action["condition"])) {
  683. if ($action["condition"] === "if") {
  684. $actionLine[] = "@if(";
  685. } else {
  686. $actionLine[] = "@if(!";
  687. }
  688. $actionLine[] = "\$record->{$action["field"]})";
  689. }
  690. $actionLine[] = "<a " .
  691. 'up-modal=".form-contents" up-width="800" up-history="false" ' .
  692. "href='{$action["link"]}' title='{$action["label"]}' class='ml-2 text-dark font-weight-normal'>" .
  693. "<i class='fa fa-{$action["icon"]} text-sm'></i>" .
  694. "</a>";
  695. if (isset($action["condition"])) {
  696. $actionLine[] = "@endif";
  697. }
  698. $actionLine = implode(" ", $actionLine);
  699. $groupHtml = str_replace("<!-- __GROUP_ACTION__ -->", $actionLine, $groupHtml);
  700. }
  701. $fields = [];
  702. foreach ($group["fields"] as $field) {
  703. $columnTitle = $this->snakeToTitleCase($field);
  704. $columnValue = "<?= \$record->$field ?>";
  705. $hasLink = false;
  706. $linkTarget = null;
  707. $actions = [];
  708. // check if this column has column spec
  709. if(isset($method->columns[$field])) {
  710. $columnTitle = $method->columns[$field]["label"];
  711. if(isset($method->columns[$field]["query"])) {
  712. $columnValue = "<?php \$_r = \Illuminate\Support\Facades\DB::" .
  713. "select(\"{$method->columns[$field]["query"]}\");\n" .
  714. "echo (\$_r && count(\$_r)) ? \$_r[0]->result : '-'; ?>";
  715. }
  716. if(isset($method->columns[$field]["link"])) {
  717. $hasLink = true;
  718. $linkTarget = $method->columns[$field]["link"];
  719. }
  720. }
  721. if(isset($method->columnActions[$field])) {
  722. foreach($method->columnActions[$field] as $action) {
  723. $actionLine = [];
  724. if(isset($action["condition"])) {
  725. if($action["condition"] === "if") {
  726. $actionLine[] = "@if(";
  727. }
  728. else {
  729. $actionLine[] = "@if(!";
  730. }
  731. $actionLine[] = "\$record->{$action["field"]})";
  732. }
  733. $actionLine[] = "<a " .
  734. 'up-modal=".form-contents" up-width="800" up-history="false" ' .
  735. "href='{$action["link"]}' title='{$action["label"]}' class='ml-2 text-dark font-weight-normal'>" .
  736. "<i class='fa fa-{$action["icon"]} text-sm'></i>" .
  737. "</a>";
  738. if(isset($action["condition"])) {
  739. $actionLine[] = "@endif";
  740. }
  741. $actionLine = implode(" ", $actionLine);
  742. $actions[] = $actionLine;
  743. }
  744. }
  745. $actions = implode("\n", $actions);
  746. $fields[] = "<tr>" .
  747. "<td class=\"w-25 px-2 text-secondary border-right\">$columnTitle</td>" .
  748. "<td class=\"w-75 px-2 font-weight-bold\">" .
  749. ($hasLink ? "<a href=\"{$linkTarget}\">" : "") .
  750. $columnValue .
  751. ($hasLink ? "</a>" : "") .
  752. $actions .
  753. "</td>" .
  754. "</tr>";
  755. }
  756. $groupHtml = str_replace("<!-- __GROUP_FIELDS__ -->", implode("\n", $fields), $groupHtml);
  757. $groupsHtml[] = $groupHtml;
  758. }
  759. $groupsHtml = implode("\n", $groupsHtml);
  760. $html = str_replace("<!-- _GROUPS_ -->", $groupsHtml, $html);
  761. }
  762. $text = str_replace("_SUB_VIEW_", $html, $text);
  763. $text = str_replace("_ACTION_LINKS_VIEW_", "{$controller->root}/{$controller->parentControllerName}/actions", $text);
  764. }
  765. else {
  766. $text = str_replace("_SUB_VIEW_",
  767. "<h5 class='py-3 border-bottom'>" .
  768. $this->camelToTitleCase($this->snakeToTitleCase($method->name)) . "</h5>" .
  769. "Controller: <b>{$controller->name}</b><br>" .
  770. "Action: <b>{$method->name}()</b><br>" .
  771. "View: <b>{$controller->root}/{$controller->name}/{$method->name}.blade.php</b><br><br>",
  772. $text);
  773. }
  774. return $text;
  775. }
  776. public function saveAddNewView(GenController $controller, GenControllerMethod $method)
  777. {
  778. $text = file_get_contents(base_path('generatecv/tree-templates/add_new.template.blade.php'));
  779. $text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
  780. $text = str_replace("_ADD_TITLE_", $this->snakeToTitleCase($method->name), $text);
  781. $text = str_replace("_API_", "/api/{$this->snakeToCamelCase($controller->dbTable)}/{$method->api}", $text);
  782. $text = str_replace("_BACK_ROUTE_", "{$controller->name}-index", $text);
  783. $text = str_replace("_RETURN_ROUTE_", "{$controller->name}-{$method->name}", $text);
  784. $columns = $method->data;
  785. $fields = [];
  786. foreach ($columns as $column) {
  787. $fields[] = $this->generateFormField($column);
  788. }
  789. $text = str_replace("<!-- _SCAFFOLD_FIELDS_ -->", implode("\n", $fields), $text);
  790. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  791. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  792. }
  793. public function saveRoutes() {
  794. $lines = ["// --- {$this->root}: {$this->name} --- //"];
  795. foreach ($this->methods as $method) {
  796. // FORMAT:
  797. // Route::get('/foo/bar/{uid}', 'FooController@bar')->name('foo-action');
  798. $lines[] = "Route::get('{$method->route}', '{$this->name}_Controller@{$method->name}')->name('{$this->name}-{$method->name}');";
  799. }
  800. $lines[] = '';
  801. $lines[] = '';
  802. file_put_contents(base_path("routes/generated.php"), implode("\n", $lines), FILE_APPEND);
  803. }
  804. public function log() {
  805. $this->w('');
  806. $this->w("Controller: app/Http/Controllers/{$this->name}_Controller");
  807. $this->w("Table: {$this->dbTable}");
  808. $this->w('---------------------------------------------');
  809. foreach ($this->methods as $method) {
  810. $this->w('Rout: ' . $method->route, 1);
  811. $this->w('Meth: ' . $method->name . '($request' . ($method->hasUID ? ', $uid' : '') . ')', 1);
  812. if(!empty($method->data)) $this->w('Data: ' . implode(", ", $method->data), 1);
  813. $this->w('Exit: ' . $method->exitURL, 1);
  814. $this->w('View: ' . $method->viewURL, 1);
  815. if(!$method->redirect) {
  816. $this->w('View: ' . resource_path("views/{$this->root}/{$this->name}/{$method->name}.blade.php"), 1);
  817. }
  818. else {
  819. $this->w('Redi: ' . $method->redirect, 1);
  820. }
  821. $this->w('---------------------------------------------');
  822. }
  823. }
  824. public function w($line, $level = -1) {
  825. for($i=0; $i<$level; $i++) echo "\t";
  826. echo "$line\n";
  827. }
  828. public function snakeToTitleCase($text) {
  829. $text = preg_replace("/^(SUB|ACTION)_/", "", $text);
  830. return ucwords(str_replace("_", " ", $text));
  831. }
  832. public function camelToTitleCase($text) {
  833. $text = preg_replace("/^(SUB|ACTION)_/", "", $text);
  834. $text = preg_replace("/([a-z])([A-Z0-9])/", "$1 $2", $text);
  835. return ucwords($text);
  836. }
  837. public function snakeToCamelCase($text) {
  838. $text = ucwords(str_replace("_", " ", $text));
  839. $text = str_replace(" ", "", $text);
  840. $text[0] = strtolower($text[0]);
  841. return $text;
  842. }
  843. private function file_force_contents($dir, $contents){
  844. $dir = str_replace("\\", "/", $dir);
  845. $dir = str_replace( '//', '/', $dir);
  846. $parts = explode('/', $dir);
  847. $file = array_pop($parts);
  848. $dir = '';
  849. foreach($parts as $part) {
  850. if(strlen($part) === 0 || $part[strlen($part) - 1] !== ':') {
  851. if(!is_dir($dir .= "/$part")) mkdir($dir);
  852. }
  853. }
  854. file_put_contents("$dir/$file", $contents);
  855. }
  856. private function generateFormField($line) {
  857. $tokens = explode("=", $line);
  858. $default = false;
  859. if(count($tokens) > 1) {
  860. $default = $tokens[1];
  861. }
  862. $tokens = explode(":", $tokens[0]);
  863. $name = $tokens[0];
  864. $required = false;
  865. if($name[strlen($name) - 1] === '*') { // required field?
  866. $required = true;
  867. $name = substr($name, 0, strlen($name) - 1);
  868. }
  869. $display = $name;
  870. $dotPos = strpos($name, ".");
  871. if($dotPos !== FALSE) {
  872. $display = substr($name, $dotPos + 1);
  873. }
  874. $display = preg_replace('/uid$/i', "", $display);
  875. $type = "text";
  876. $options = [];
  877. if(count($tokens) > 1) {
  878. $type = $tokens[1];
  879. switch ($type) {
  880. case "select":
  881. $options = explode(",", $tokens[2]);
  882. break;
  883. case "record":
  884. $options['table'] = $tokens[2];
  885. $parts = explode(",", $tokens[3]);
  886. $options['valueField'] = $parts[0];
  887. $options['displayField'] = $parts[1];
  888. break;
  889. }
  890. }
  891. if($type !== 'hidden' && $type !== 'bool') {
  892. $code[] = "<div class='form-group mb-3'>";
  893. $code[] = "<label class='control-label'>{$this->camelToTitleCase($this->snakeToTitleCase($display))} " .
  894. ($required ? "*" : "") .
  895. "</label>";
  896. }
  897. $valueLine = "value='{{ old('$name') ? old('$name') : " . ($default ? "\$record->$default" : '\'\'') . " }}' ";
  898. switch ($type) {
  899. case "select":
  900. $code[] = "<select class='form-control' name='$name' " . $valueLine .
  901. ($required ? "required" : "") .
  902. ">";
  903. $code[] = "<option value=''>-- Select --</option>";
  904. foreach ($options as $o) {
  905. $code[] = "<option " .
  906. "<?= '$o' === (old('$name') ? old('$name') : " . ($default ? "\$record->$default" : "''") . ") ? 'selected' : '' ?> " .
  907. "value='$o'>$o</option>";
  908. }
  909. $code[] = "</select>";
  910. break;
  911. case "record":
  912. $code[] = "<select class='form-control' name='$name' " . $valueLine .
  913. ($required ? "required" : "") .
  914. ">";
  915. $code[] = "<option value=''>-- Select --</option>";
  916. $code[] = "<?php \$dbOptions = \Illuminate\Support\Facades\DB::table('{$options['table']}')->get(); ?>";
  917. $code[] = "<?php foreach(\$dbOptions as \$o): ?>";
  918. $code[] = "<option " .
  919. "<?= \$o->{$options['valueField']} === (old('$name') ? old('$name') : " . ($default ? "\$record->$default" : "''") . ") ? 'selected' : '' ?> " .
  920. "value='<?= \$o->{$options['valueField']} ?>'><?= \$o->{$options['displayField']} ?> (<?= \$o->{$options['valueField']} ?>)</option>";
  921. $code[] = "<?php endforeach; ?>";
  922. $code[] = "</select>";
  923. break;
  924. case "bool":
  925. $code[] = "<div class='form-group mb-3'>";
  926. $code[] = "<label class='control-label'>{$this->camelToTitleCase($this->snakeToTitleCase($display))} " .
  927. ($required ? "*" : "");
  928. $code[] = "<input class='ml-2' type='checkbox' name='$name' " .
  929. ($required ? "required" : "") .
  930. ">";
  931. $code[] = "</label>";
  932. break;
  933. default:
  934. $code[] = "<input class='form-control' type='$type' name='$name' " . $valueLine .
  935. ($required ? "required" : "") .
  936. ">";
  937. }
  938. if($type !== 'hidden') {
  939. $code[] = "</div>";
  940. }
  941. return implode("\n", $code);
  942. }
  943. }
  944. class GenControllerMethod {
  945. public $name;
  946. public $route;
  947. public $hasUID = false;
  948. public $redirect = false;
  949. public $type = '';
  950. public $data = [];
  951. public $parentSub = false;
  952. public $childAddRoute = false;
  953. public $table = false;
  954. public $api = 'create';
  955. public $show = null;
  956. public $viewURL = false;
  957. public $exitURL = false;
  958. public $showLink = true;
  959. public $incType = null;
  960. public $incFields = null;
  961. public $viewLinkField = 'uid';
  962. public $columns = [];
  963. public $columnActions = [];
  964. public $groups = [];
  965. public $dashboard = false;
  966. public $noActionLinks = false;
  967. public function __construct($name, $route)
  968. {
  969. $this->name = $name;
  970. $this->route = $route;
  971. if(strpos($this->route, "{uid}") !== FALSE) {
  972. $this->hasUID = true;
  973. }
  974. }
  975. public function setIncFields($type, $fields) {
  976. $this->incType = $type;
  977. $this->incFields = $fields;
  978. for ($i = 0; $i < count($this->incFields); $i++) {
  979. if($this->incFields[$i][0] === '@') {
  980. $this->incFields[$i] = substr($this->incFields[$i], 1);
  981. $this->viewLinkField = $this->incFields[$i];
  982. }
  983. }
  984. }
  985. public function setColumnSpec($line, $link, $recordVariable) {
  986. // ally_pro_id:Ally Pro:select name_display as 'result' from pro where id = $ally_pro_id limit 1
  987. $parts = explode(":", $line);
  988. $spec = [
  989. "label" => $parts[1]
  990. ];
  991. if(count($parts) > 2) {
  992. $query = $parts[2];
  993. $query = preg_replace("/\\$([a-zA-Z0-9_]+)/", "\" . ($$recordVariable->$1 ? $$recordVariable->$1 : -1) . \"", $query);
  994. $spec['query'] = $query;
  995. }
  996. if($link) {
  997. $spec['link'] = preg_replace("/\\$([a-zA-Z0-9_]+)/", "<?= \$$recordVariable->$1 ?>", $link);
  998. }
  999. $this->columns[$parts[0]] = $spec;
  1000. }
  1001. public function addGroup($line, $link) {
  1002. // Basic Details:id,uid,created_at,clients_count
  1003. $parts = explode(":", $line);
  1004. $group = [
  1005. "name" => $parts[0],
  1006. "fields" => explode(",", $parts[1])
  1007. ];
  1008. // is there a group action
  1009. if(count($parts) > 2) {
  1010. $actionParts = ["-"];
  1011. for($i = 2; $i < count($parts); $i++) {
  1012. $actionParts[] = $parts[$i];
  1013. }
  1014. $actionParts = implode(":", $actionParts);
  1015. $group["action"] = $this->addAction($actionParts, $link, true);
  1016. }
  1017. $this->groups[] = $group;
  1018. $this->dashboard = true;
  1019. }
  1020. public function addAction($line, $link, $group = false) {
  1021. // is_active:Deactivate:deactivate:edit:if:is_active
  1022. $parts = explode(":", $line);
  1023. $action = [
  1024. "label" => $parts[1],
  1025. "icon" => isset($parts[2]) ? $parts[2] : 'edit'
  1026. ];
  1027. if(count($parts) >= 5 ) {
  1028. $action["condition"] = $parts[3];
  1029. $action["field"] = $parts[4];
  1030. }
  1031. if($link) {
  1032. $action['link'] = preg_replace("/\\$([a-zA-Z0-9_]+)/", "<?= \$record->$1 ?>", $link);
  1033. }
  1034. if(!$group) {
  1035. if(!isset($this->columnActions[$parts[0]])) {
  1036. $this->columnActions[$parts[0]] = [];
  1037. }
  1038. $this->columnActions[$parts[0]][] = $action;
  1039. }
  1040. return $action;
  1041. }
  1042. }