GenerateTreeCommand.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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));
  222. }
  223. }
  224. else if(strpos($line, "!act:") === 0) { // !act:
  225. if (!empty($currentMethod)) {
  226. $currentMethod->addColumnAction(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. $fields = [];
  680. foreach ($group["fields"] as $field) {
  681. $columnTitle = $this->snakeToTitleCase($field);
  682. $columnValue = "<?= \$record->$field ?>";
  683. $hasLink = false;
  684. $linkTarget = null;
  685. $actions = [];
  686. // check if this column has column spec
  687. if(isset($method->columns[$field])) {
  688. $columnTitle = $method->columns[$field]["label"];
  689. if(isset($method->columns[$field]["query"])) {
  690. $columnValue = "<?php \$_r = \Illuminate\Support\Facades\DB::" .
  691. "select(\"{$method->columns[$field]["query"]}\");\n" .
  692. "echo (\$_r && count(\$_r)) ? \$_r[0]->result : '-'; ?>";
  693. }
  694. if(isset($method->columns[$field]["link"])) {
  695. $hasLink = true;
  696. $linkTarget = $method->columns[$field]["link"];
  697. }
  698. }
  699. /*
  700. "is_active" => array:1 [
  701. 0 => array:5 [
  702. "label" => "Deactivate"
  703. "icon" => "edit"
  704. "condition" => "if"
  705. "field" => "is_active"
  706. ]
  707. ]
  708. */
  709. if(isset($method->columnActions[$field])) {
  710. foreach($method->columnActions[$field] as $action) {
  711. $actionLine = [];
  712. if(isset($action["condition"])) {
  713. if($action["condition"] === "if") {
  714. $actionLine[] = "@if(";
  715. }
  716. else {
  717. $actionLine[] = "@if(!";
  718. }
  719. $actionLine[] = "\$record->{$action["field"]})";
  720. }
  721. $actionLine[] = "<a " .
  722. 'up-modal=".form-contents" up-width="800" up-history="false" ' .
  723. "href='{$action["link"]}' title='{$action["label"]}' class='ml-2 text-dark font-weight-normal'>" .
  724. "<i class='fa fa-{$action["icon"]} text-sm'></i>" .
  725. "</a>";
  726. if(isset($action["condition"])) {
  727. $actionLine[] = "@endif";
  728. }
  729. $actionLine = implode(" ", $actionLine);
  730. $actions[] = $actionLine;
  731. }
  732. }
  733. $actions = implode("\n", $actions);
  734. $fields[] = "<tr>" .
  735. "<td class=\"w-25 px-2 text-secondary border-right\">$columnTitle</td>" .
  736. "<td class=\"w-75 px-2 font-weight-bold\">" .
  737. ($hasLink ? "<a href=\"{$linkTarget}\">" : "") .
  738. $columnValue .
  739. ($hasLink ? "</a>" : "") .
  740. $actions .
  741. "</td>" .
  742. "</tr>";
  743. }
  744. $groupHtml = str_replace("<!-- __GROUP_FIELDS__ -->", implode("\n", $fields), $groupHtml);
  745. $groupsHtml[] = $groupHtml;
  746. }
  747. $groupsHtml = implode("\n", $groupsHtml);
  748. $html = str_replace("<!-- _GROUPS_ -->", $groupsHtml, $html);
  749. }
  750. $text = str_replace("_SUB_VIEW_", $html, $text);
  751. $text = str_replace("_ACTION_LINKS_VIEW_", "{$controller->root}/{$controller->parentControllerName}/actions", $text);
  752. }
  753. else {
  754. $text = str_replace("_SUB_VIEW_",
  755. "<h5 class='py-3 border-bottom'>" .
  756. $this->camelToTitleCase($this->snakeToTitleCase($method->name)) . "</h5>" .
  757. "Controller: <b>{$controller->name}</b><br>" .
  758. "Action: <b>{$method->name}()</b><br>" .
  759. "View: <b>{$controller->root}/{$controller->name}/{$method->name}.blade.php</b><br><br>",
  760. $text);
  761. }
  762. return $text;
  763. }
  764. public function saveAddNewView(GenController $controller, GenControllerMethod $method)
  765. {
  766. $text = file_get_contents(base_path('generatecv/tree-templates/add_new.template.blade.php'));
  767. $text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
  768. $text = str_replace("_ADD_TITLE_", $this->snakeToTitleCase($method->name), $text);
  769. $text = str_replace("_API_", "/api/{$this->snakeToCamelCase($controller->dbTable)}/{$method->api}", $text);
  770. $text = str_replace("_BACK_ROUTE_", "{$controller->name}-index", $text);
  771. $text = str_replace("_RETURN_ROUTE_", "{$controller->name}-{$method->name}", $text);
  772. $columns = $method->data;
  773. $fields = [];
  774. foreach ($columns as $column) {
  775. $fields[] = $this->generateFormField($column);
  776. }
  777. $text = str_replace("<!-- _SCAFFOLD_FIELDS_ -->", implode("\n", $fields), $text);
  778. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  779. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  780. }
  781. public function saveRoutes() {
  782. $lines = ["// --- {$this->root}: {$this->name} --- //"];
  783. foreach ($this->methods as $method) {
  784. // FORMAT:
  785. // Route::get('/foo/bar/{uid}', 'FooController@bar')->name('foo-action');
  786. $lines[] = "Route::get('{$method->route}', '{$this->name}_Controller@{$method->name}')->name('{$this->name}-{$method->name}');";
  787. }
  788. $lines[] = '';
  789. $lines[] = '';
  790. file_put_contents(base_path("routes/generated.php"), implode("\n", $lines), FILE_APPEND);
  791. }
  792. public function log() {
  793. $this->w('');
  794. $this->w("Controller: app/Http/Controllers/{$this->name}_Controller");
  795. $this->w("Table: {$this->dbTable}");
  796. $this->w('---------------------------------------------');
  797. foreach ($this->methods as $method) {
  798. $this->w('Rout: ' . $method->route, 1);
  799. $this->w('Meth: ' . $method->name . '($request' . ($method->hasUID ? ', $uid' : '') . ')', 1);
  800. if(!empty($method->data)) $this->w('Data: ' . implode(", ", $method->data), 1);
  801. $this->w('Exit: ' . $method->exitURL, 1);
  802. $this->w('View: ' . $method->viewURL, 1);
  803. if(!$method->redirect) {
  804. $this->w('View: ' . resource_path("views/{$this->root}/{$this->name}/{$method->name}.blade.php"), 1);
  805. }
  806. else {
  807. $this->w('Redi: ' . $method->redirect, 1);
  808. }
  809. $this->w('---------------------------------------------');
  810. }
  811. }
  812. public function w($line, $level = -1) {
  813. for($i=0; $i<$level; $i++) echo "\t";
  814. echo "$line\n";
  815. }
  816. public function snakeToTitleCase($text) {
  817. $text = preg_replace("/^(SUB|ACTION)_/", "", $text);
  818. return ucwords(str_replace("_", " ", $text));
  819. }
  820. public function camelToTitleCase($text) {
  821. $text = preg_replace("/^(SUB|ACTION)_/", "", $text);
  822. $text = preg_replace("/([a-z])([A-Z0-9])/", "$1 $2", $text);
  823. return ucwords($text);
  824. }
  825. public function snakeToCamelCase($text) {
  826. $text = ucwords(str_replace("_", " ", $text));
  827. $text = str_replace(" ", "", $text);
  828. $text[0] = strtolower($text[0]);
  829. return $text;
  830. }
  831. private function file_force_contents($dir, $contents){
  832. $dir = str_replace("\\", "/", $dir);
  833. $dir = str_replace( '//', '/', $dir);
  834. $parts = explode('/', $dir);
  835. $file = array_pop($parts);
  836. $dir = '';
  837. foreach($parts as $part) {
  838. if(strlen($part) === 0 || $part[strlen($part) - 1] !== ':') {
  839. if(!is_dir($dir .= "/$part")) mkdir($dir);
  840. }
  841. }
  842. file_put_contents("$dir/$file", $contents);
  843. }
  844. private function generateFormField($line) {
  845. $tokens = explode("=", $line);
  846. $default = false;
  847. if(count($tokens) > 1) {
  848. $default = $tokens[1];
  849. }
  850. $tokens = explode(":", $tokens[0]);
  851. $name = $tokens[0];
  852. $required = false;
  853. if($name[strlen($name) - 1] === '*') { // required field?
  854. $required = true;
  855. $name = substr($name, 0, strlen($name) - 1);
  856. }
  857. $display = $name;
  858. $dotPos = strpos($name, ".");
  859. if($dotPos !== FALSE) {
  860. $display = substr($name, $dotPos + 1);
  861. }
  862. $display = preg_replace('/uid$/i', "", $display);
  863. $type = "text";
  864. $options = [];
  865. if(count($tokens) > 1) {
  866. $type = $tokens[1];
  867. switch ($type) {
  868. case "select":
  869. $options = explode(",", $tokens[2]);
  870. break;
  871. case "record":
  872. $options['table'] = $tokens[2];
  873. $parts = explode(",", $tokens[3]);
  874. $options['valueField'] = $parts[0];
  875. $options['displayField'] = $parts[1];
  876. break;
  877. }
  878. }
  879. if($type !== 'hidden' && $type !== 'bool') {
  880. $code[] = "<div class='form-group mb-3'>";
  881. $code[] = "<label class='control-label'>{$this->camelToTitleCase($this->snakeToTitleCase($display))} " .
  882. ($required ? "*" : "") .
  883. "</label>";
  884. }
  885. $valueLine = "value='{{ old('$name') ? old('$name') : " . ($default ? "\$record->$default" : '\'\'') . " }}' ";
  886. switch ($type) {
  887. case "select":
  888. $code[] = "<select class='form-control' name='$name' " . $valueLine .
  889. ($required ? "required" : "") .
  890. ">";
  891. $code[] = "<option value=''>-- Select --</option>";
  892. foreach ($options as $o) {
  893. $code[] = "<option " .
  894. "<?= '$o' === (old('$name') ? old('$name') : " . ($default ? "\$record->$default" : "''") . ") ? 'selected' : '' ?> " .
  895. "value='$o'>$o</option>";
  896. }
  897. $code[] = "</select>";
  898. break;
  899. case "record":
  900. $code[] = "<select class='form-control' name='$name' " . $valueLine .
  901. ($required ? "required" : "") .
  902. ">";
  903. $code[] = "<option value=''>-- Select --</option>";
  904. $code[] = "<?php \$dbOptions = \Illuminate\Support\Facades\DB::table('{$options['table']}')->get(); ?>";
  905. $code[] = "<?php foreach(\$dbOptions as \$o): ?>";
  906. $code[] = "<option " .
  907. "<?= \$o->{$options['valueField']} === (old('$name') ? old('$name') : " . ($default ? "\$record->$default" : "''") . ") ? 'selected' : '' ?> " .
  908. "value='<?= \$o->{$options['valueField']} ?>'><?= \$o->{$options['displayField']} ?> (<?= \$o->{$options['valueField']} ?>)</option>";
  909. $code[] = "<?php endforeach; ?>";
  910. $code[] = "</select>";
  911. break;
  912. case "bool":
  913. $code[] = "<div class='form-group mb-3'>";
  914. $code[] = "<label class='control-label'>{$this->camelToTitleCase($this->snakeToTitleCase($display))} " .
  915. ($required ? "*" : "");
  916. $code[] = "<input class='ml-2' type='checkbox' name='$name' " .
  917. ($required ? "required" : "") .
  918. ">";
  919. $code[] = "</label>";
  920. break;
  921. default:
  922. $code[] = "<input class='form-control' type='$type' name='$name' " . $valueLine .
  923. ($required ? "required" : "") .
  924. ">";
  925. }
  926. if($type !== 'hidden') {
  927. $code[] = "</div>";
  928. }
  929. return implode("\n", $code);
  930. }
  931. }
  932. class GenControllerMethod {
  933. public $name;
  934. public $route;
  935. public $hasUID = false;
  936. public $redirect = false;
  937. public $type = '';
  938. public $data = [];
  939. public $parentSub = false;
  940. public $childAddRoute = false;
  941. public $table = false;
  942. public $api = 'create';
  943. public $show = null;
  944. public $viewURL = false;
  945. public $exitURL = false;
  946. public $showLink = true;
  947. public $incType = null;
  948. public $incFields = null;
  949. public $viewLinkField = 'uid';
  950. public $columns = [];
  951. public $columnActions = [];
  952. public $groups = [];
  953. public $dashboard = false;
  954. public $noActionLinks = false;
  955. public function __construct($name, $route)
  956. {
  957. $this->name = $name;
  958. $this->route = $route;
  959. if(strpos($this->route, "{uid}") !== FALSE) {
  960. $this->hasUID = true;
  961. }
  962. }
  963. public function setIncFields($type, $fields) {
  964. $this->incType = $type;
  965. $this->incFields = $fields;
  966. for ($i = 0; $i < count($this->incFields); $i++) {
  967. if($this->incFields[$i][0] === '@') {
  968. $this->incFields[$i] = substr($this->incFields[$i], 1);
  969. $this->viewLinkField = $this->incFields[$i];
  970. }
  971. }
  972. }
  973. public function setColumnSpec($line, $link, $recordVariable) {
  974. // ally_pro_id:Ally Pro:select name_display as 'result' from pro where id = $ally_pro_id limit 1
  975. $parts = explode(":", $line);
  976. $spec = [
  977. "label" => $parts[1]
  978. ];
  979. if(count($parts) > 2) {
  980. $query = $parts[2];
  981. $query = preg_replace("/\\$([a-zA-Z0-9_]+)/", "\" . ($$recordVariable->$1 ? $$recordVariable->$1 : -1) . \"", $query);
  982. $spec['query'] = $query;
  983. }
  984. if($link) {
  985. $spec['link'] = preg_replace("/\\$([a-zA-Z0-9_]+)/", "<?= \$$recordVariable->$1 ?>", $link);
  986. }
  987. $this->columns[$parts[0]] = $spec;
  988. }
  989. public function addGroup($line) {
  990. // Basic Details:id,uid,created_at,clients_count
  991. $parts = explode(":", $line);
  992. $this->groups[] = [
  993. "name" => $parts[0],
  994. "fields" => explode(",", $parts[1])
  995. ];
  996. $this->dashboard = true;
  997. }
  998. public function addColumnAction($line, $link) {
  999. // is_active:Deactivate:deactivate:edit:if:is_active
  1000. $parts = explode(":", $line);
  1001. $action = [
  1002. "label" => $parts[1],
  1003. "icon" => isset($parts[2]) ? $parts[2] : 'edit'
  1004. ];
  1005. if(count($parts) >= 5 ) {
  1006. $action["condition"] = $parts[3];
  1007. $action["field"] = $parts[4];
  1008. }
  1009. if($link) {
  1010. $action['link'] = preg_replace("/\\$([a-zA-Z0-9_]+)/", "<?= \$record->$1 ?>", $link);
  1011. }
  1012. if(!isset($this->columnActions[$parts[0]])) {
  1013. $this->columnActions[$parts[0]] = [];
  1014. }
  1015. $this->columnActions[$parts[0]][] = $action;
  1016. }
  1017. }