Gem.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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>' . nl2br($_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. case 'Multi Checkbox with Other':
  109. // start
  110. $output .= $this->ln('<div class="mt-3">', $_level + 1);
  111. foreach ($_node->Options as $option) {
  112. $output .= $this->ln('<label class="d-flex align-items-center mb-1 mr-2">', $_level + 2);
  113. $output .= $this->ln('<input name="' . $key . '[]" value="' . $option . '" type="checkbox" class="mr-1">', $_level + 3);
  114. $output .= $this->ln('<span>' . $option . '</span>', $_level + 3);
  115. $output .= $this->ln('</label>', $_level + 2);
  116. }
  117. // OTHER
  118. $output .= $this->ln('<input name="' . $key . '_other" type="text" class="form-control form-control-sm my-3" placeholder="Other">', $_level + 2);
  119. // end
  120. $output .= $this->ln('</div>', $_level + 1);
  121. break;
  122. case 'Accept':
  123. // start
  124. $output .= $this->ln('<div class="d-flex align-items-center">', $_level + 1);
  125. // YES
  126. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $_level + 2);
  127. $output .= $this->ln('<input name="' . $key . '" type="radio" value="ACCEPT" class="mr-1">', $_level + 3);
  128. $output .= $this->ln('<span>Accept</span>', $_level + 3);
  129. $output .= $this->ln('</label>', $_level + 2);
  130. // NO
  131. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $_level + 2);
  132. $output .= $this->ln('<input name="' . $key . '" type="radio" value="REJECT" class="mr-1">', $_level + 3);
  133. $output .= $this->ln('<span>Reject</span>', $_level + 3);
  134. $output .= $this->ln('</label>', $_level + 2);
  135. // end
  136. $output .= $this->ln('</div>', $_level + 1);
  137. break;
  138. default:
  139. dump("Unknown type: {$_node->T}");
  140. break;
  141. }
  142. }
  143. // subs
  144. if (!!@$_node->S) {
  145. $output .= '<div class="subs pl-4">';
  146. foreach ($_node->S as $sub) {
  147. $output .= $this->gem($sub, $key, $_level + 1);
  148. }
  149. $output .= '</div>';
  150. }
  151. // close container
  152. $output .= $this->ln('</div>', $_level + 1);
  153. $output .= $this->nl();
  154. return $output;
  155. }
  156. public function ln($_line, $_indent = 1, $_nlAfter = true)
  157. {
  158. return str_repeat(" ", $_indent * 4) . $_line . ($_nlAfter ? "\n" : '');
  159. }
  160. public function nl()
  161. {
  162. return "\n";
  163. }
  164. }