Gem.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. class Gem extends Command
  5. {
  6. /**
  7. * The name and signature of the console command.
  8. *
  9. * @var string
  10. */
  11. protected $signature = 'gem
  12. {path:path-in-gem}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Command description';
  19. public $output = '';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. global $argv;
  37. $in = json_decode(file_get_contents(base_path("gem/forms/{$argv[2]}/spec.json")));
  38. $outPath = base_path("gem/forms/{$argv[2]}/build");
  39. $this->output = '<div class="gem-nodes">';
  40. foreach ($in as $node) {
  41. $this->output .= $this->gem($node);
  42. }
  43. $this->output .= '</div>';
  44. file_put_contents(base_path("gem/forms/{$argv[2]}/build/form.blade.php"), $this->output);
  45. echo "OK";
  46. }
  47. public function gem($_node, $_parentKey = false, $_level = 0)
  48. {
  49. $output = '';
  50. $key = ($_parentKey ? $_parentKey . '__' : '') . $_node->K;
  51. // start container
  52. $output .= $this->ln('<div ' .
  53. 'class="my-3 node node-level-' . $_level . '" ' .
  54. 'data-key="' . $key . '">', $_level + 1);
  55. // label
  56. $output .= $this->ln('<label>' . $_node->Q . '</label>', $_level + 2);
  57. // input
  58. if (!!@$_node->T) {
  59. switch ($_node->T) {
  60. case 'YNUM':
  61. // start
  62. $output .= $this->ln('<div class="d-flex align-items-center">', $_level + 1);
  63. // YES
  64. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $_level + 2);
  65. $output .= $this->ln('<input name="' . $key . '" type="radio" value="YES" class="mr-1">', $_level + 3);
  66. $output .= $this->ln('<span>Yes</span>', $_level + 3);
  67. $output .= $this->ln('</label>', $_level + 2);
  68. // NO
  69. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $_level + 2);
  70. $output .= $this->ln('<input name="' . $key . '" type="radio" value="NO" class="mr-1">', $_level + 3);
  71. $output .= $this->ln('<span>No</span>', $_level + 3);
  72. $output .= $this->ln('</label>', $_level + 2);
  73. // NO
  74. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $_level + 2);
  75. $output .= $this->ln('<input name="' . $key . '" type="radio" value="UNKNOWN" class="mr-1">', $_level + 3);
  76. $output .= $this->ln('<span>Unknown</span>', $_level + 3);
  77. $output .= $this->ln('</label>', $_level + 2);
  78. // MEMO
  79. $output .= $this->ln('<input name="' . $key . '_memo" type="text" class="form-control form-control-sm" placeholder="Memo">', $_level + 2);
  80. // end
  81. $output .= $this->ln('</div>', $_level + 1);
  82. break;
  83. case 'SRV':
  84. // start
  85. $output .= $this->ln('<div class="d-flex align-items-center">', $_level + 1);
  86. // VALUE
  87. $output .= $this->ln('<input name="' . $key . '" type="text" class="form-control form-control-sm" placeholder="Answer">', $_level + 2);
  88. // UNKNOWN
  89. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 ml-3 mr-2">', $_level + 2);
  90. $output .= $this->ln('<input name="' . $key . '" type="checkbox" class="mr-1">', $_level + 3);
  91. $output .= $this->ln('<span>Unknown</span>', $_level + 3);
  92. $output .= $this->ln('</label>', $_level + 2);
  93. // MEMO
  94. $output .= $this->ln('<input name="' . $key . '_memo" type="text" class="form-control form-control-sm" placeholder="Memo">', $_level + 2);
  95. // end
  96. $output .= $this->ln('</div>', $_level + 1);
  97. break;
  98. case 'Text with Memo':
  99. // start
  100. $output .= $this->ln('<div class="d-flex align-items-center">', $_level + 1);
  101. // VALUE
  102. $output .= $this->ln('<input name="' . $key . '" type="text" class="form-control form-control-sm mr-2" placeholder="Answer">', $_level + 2);
  103. // MEMO
  104. $output .= $this->ln('<input name="' . $key . '_memo" type="text" class="form-control form-control-sm" placeholder="Memo">', $_level + 2);
  105. // end
  106. $output .= $this->ln('</div>', $_level + 1);
  107. break;
  108. default:
  109. dump("Unknown type: {$_node->T}");
  110. break;
  111. }
  112. }
  113. // subs
  114. if (!!@$_node->S) {
  115. $output .= '<div class="subs pl-4">';
  116. foreach ($_node->S as $sub) {
  117. $output .= $this->gem($sub, $key, $_level + 1);
  118. }
  119. $output .= '</div>';
  120. }
  121. // close container
  122. $output .= $this->ln('</div>', $_level + 1);
  123. $output .= $this->nl();
  124. return $output;
  125. }
  126. public function ln($_line, $_indent = 1, $_nlAfter = true)
  127. {
  128. return str_repeat(" ", $_indent * 4) . $_line . ($_nlAfter ? "\n" : '');
  129. }
  130. public function nl()
  131. {
  132. return "\n";
  133. }
  134. }