Gem.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. [{--write-to-storage}]';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. public $output = '';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. global $argv;
  38. $in = json_decode(file_get_contents(base_path("gem/forms/{$argv[2]}/spec.json")));
  39. $outPath = base_path("gem/forms/{$argv[2]}/build");
  40. $this->output = $this->ln('<div class="gem-nodes">', 1);
  41. foreach ($in as $node) {
  42. $this->output .= $this->gem($node);
  43. }
  44. $this->output .= $this->ln('</div>', 1);
  45. // write form
  46. $template = file_get_contents(base_path("gem/templates/form.blade.php"));
  47. $this->output = str_replace('<!-- __GENERATED_MARKUP__ -->', $this->output, $template);
  48. file_put_contents(base_path("gem/forms/{$argv[2]}/build/form.blade.php"), $this->output);
  49. // write processor
  50. copy(base_path("gem/templates/processor.php"), base_path("gem/forms/{$argv[2]}/build/processor.php"));
  51. // write summary
  52. copy(base_path("gem/templates/summary.php"), base_path("gem/forms/{$argv[2]}/build/summary.php"));
  53. // copy output to store/section
  54. if(in_array("--write-to-storage", $argv) !== FALSE) {
  55. copy(base_path("gem/forms/{$argv[2]}/build/form.blade.php"), base_path("storage/sections/{$argv[2]}/form.blade.php"));
  56. copy(base_path("gem/forms/{$argv[2]}/build/processor.php"), base_path("storage/sections/{$argv[2]}/processor.php"));
  57. copy(base_path("gem/forms/{$argv[2]}/build/summary.php"), base_path("storage/sections/{$argv[2]}/summary.php"));
  58. }
  59. echo "OK";
  60. }
  61. public function gem($_node, $_parentKey = false, $_level = 0)
  62. {
  63. $output = '';
  64. $key = ($_parentKey ? $_parentKey . '__' : '') . $_node->K;
  65. // start container
  66. $output .= $this->ln('<div ' .
  67. 'class="my-3 node node-level-' . $_level . '" ' .
  68. 'data-key="' . $key . '">', $_level + 1);
  69. // label
  70. $output .= $this->ln('<label>' . $this->formatText($_node->Q) . '</label>', $_level + 2);
  71. // input
  72. $fieldLevel = $_level + 1;
  73. if (!!@$_node->T) {
  74. switch ($_node->T) {
  75. case 'YNUM':
  76. // start
  77. $output .= $this->ln('<div class="d-flex align-items-center">', $fieldLevel + 1);
  78. // YES
  79. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $fieldLevel + 2);
  80. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '" type="radio" value="YES" class="mr-1">', $fieldLevel + 3);
  81. $output .= $this->ln('<span>Yes</span>', $fieldLevel + 3);
  82. $output .= $this->ln('</label>', $fieldLevel + 2);
  83. // NO
  84. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $fieldLevel + 2);
  85. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '" type="radio" value="NO" class="mr-1">', $fieldLevel + 3);
  86. $output .= $this->ln('<span>No</span>', $fieldLevel + 3);
  87. $output .= $this->ln('</label>', $fieldLevel + 2);
  88. // NO
  89. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $fieldLevel + 2);
  90. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '" type="radio" value="UNKNOWN" class="mr-1">', $fieldLevel + 3);
  91. $output .= $this->ln('<span>Unknown</span>', $fieldLevel + 3);
  92. $output .= $this->ln('</label>', $fieldLevel + 2);
  93. // MEMO
  94. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '_memo" type="text" class="form-control form-control-sm" placeholder="Memo">', $fieldLevel + 2);
  95. // end
  96. $output .= $this->ln('</div>', $fieldLevel + 1);
  97. break;
  98. case 'SRV':
  99. // start
  100. $output .= $this->ln('<div class="d-flex align-items-center">', $fieldLevel + 1);
  101. // VALUE
  102. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '" type="text" class="form-control form-control-sm" placeholder="Answer">', $fieldLevel + 2);
  103. // UNKNOWN
  104. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 ml-3 mr-2">', $fieldLevel + 2);
  105. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '" type="checkbox" class="mr-1">', $fieldLevel + 3);
  106. $output .= $this->ln('<span>Unknown</span>', $fieldLevel + 3);
  107. $output .= $this->ln('</label>', $fieldLevel + 2);
  108. // MEMO
  109. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '_memo" type="text" class="form-control form-control-sm" placeholder="Memo">', $fieldLevel + 2);
  110. // end
  111. $output .= $this->ln('</div>', $fieldLevel + 1);
  112. break;
  113. case 'Text with Memo':
  114. // start
  115. $output .= $this->ln('<div class="d-flex align-items-center">', $fieldLevel + 1);
  116. // VALUE
  117. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '" type="text" class="form-control form-control-sm mr-2" placeholder="Answer">', $fieldLevel + 2);
  118. // MEMO
  119. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '_memo" type="text" class="form-control form-control-sm" placeholder="Memo">', $fieldLevel + 2);
  120. // end
  121. $output .= $this->ln('</div>', $fieldLevel + 1);
  122. break;
  123. case 'Multi Checkbox with Other':
  124. // start
  125. $output .= $this->ln('<div class="mt-3">', $fieldLevel + 1);
  126. foreach ($_node->Options as $option) {
  127. $output .= $this->ln('<label class="d-flex align-items-center mb-1 mr-2">', $fieldLevel + 2);
  128. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '[]" value="' . $option . '" type="checkbox" class="mr-1">', $fieldLevel + 3);
  129. $output .= $this->ln('<span>' . $option . '</span>', $fieldLevel + 3);
  130. $output .= $this->ln('</label>', $fieldLevel + 2);
  131. }
  132. // OTHER
  133. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '_other" type="text" class="form-control form-control-sm my-3" placeholder="Other">', $fieldLevel + 2);
  134. // end
  135. $output .= $this->ln('</div>', $fieldLevel + 1);
  136. break;
  137. case 'Accept':
  138. // start
  139. $output .= $this->ln('<div class="d-flex align-items-center">', $fieldLevel + 1);
  140. // YES
  141. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $fieldLevel + 2);
  142. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '" type="radio" value="ACCEPT" class="mr-1">', $fieldLevel + 3);
  143. $output .= $this->ln('<span>Accept</span>', $fieldLevel + 3);
  144. $output .= $this->ln('</label>', $fieldLevel + 2);
  145. // NO
  146. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $fieldLevel + 2);
  147. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" name="' . $key . '" type="radio" value="REJECT" class="mr-1">', $fieldLevel + 3);
  148. $output .= $this->ln('<span>Reject</span>', $fieldLevel + 3);
  149. $output .= $this->ln('</label>', $fieldLevel + 2);
  150. // end
  151. $output .= $this->ln('</div>', $fieldLevel + 1);
  152. break;
  153. default:
  154. dump("Unknown type: {$_node->T}");
  155. break;
  156. }
  157. }
  158. // subs
  159. if (!!@$_node->S) {
  160. $output .= $this->ln('<div class="subs pl-4">', $_level + 2);
  161. foreach ($_node->S as $sub) {
  162. $output .= $this->gem($sub, $key, $_level + 2);
  163. }
  164. $output .= $this->ln('</div>', $_level + 2);
  165. }
  166. // close container
  167. $output .= $this->ln('</div>', $_level + 1);
  168. $output .= $this->nl();
  169. return $output;
  170. }
  171. public function formatText($_text) {
  172. $_text = preg_replace("/\{\{(.+?)\}\}/", '<span field="$1"></span>', $_text);
  173. return nl2br($_text);
  174. }
  175. public function ln($_line, $_indent = 1, $_nlAfter = true)
  176. {
  177. return str_repeat(" ", $_indent * 4) . $_line . ($_nlAfter ? "\n" : '');
  178. }
  179. public function nl()
  180. {
  181. return "\n";
  182. }
  183. }