Gem.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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)" '.
  81. "{{ @\$contentData['{$key}'] === 'YES' ? 'checked' : '' }} " .
  82. 'name="' . $key . '" type="radio" value="YES" class="mr-1">', $fieldLevel + 3);
  83. $output .= $this->ln('<span>Yes</span>', $fieldLevel + 3);
  84. $output .= $this->ln('</label>', $fieldLevel + 2);
  85. // NO
  86. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $fieldLevel + 2);
  87. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  88. "{{ @\$contentData['{$key}'] === 'NO' ? 'checked' : '' }} " .
  89. 'name="' . $key . '" type="radio" value="NO" class="mr-1">', $fieldLevel + 3);
  90. $output .= $this->ln('<span>No</span>', $fieldLevel + 3);
  91. $output .= $this->ln('</label>', $fieldLevel + 2);
  92. // NO
  93. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $fieldLevel + 2);
  94. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  95. "{{ @\$contentData['{$key}'] === 'UNKNOWN' ? 'checked' : '' }} " .
  96. 'name="' . $key . '" type="radio" value="UNKNOWN" class="mr-1">', $fieldLevel + 3);
  97. $output .= $this->ln('<span>Unknown</span>', $fieldLevel + 3);
  98. $output .= $this->ln('</label>', $fieldLevel + 2);
  99. // MEMO
  100. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  101. 'value="' . "{{ @\$contentData['{$key}_memo'] }}" . '" ' .
  102. 'name="' . $key . '_memo" type="text" class="form-control form-control-sm" placeholder="Memo">', $fieldLevel + 2);
  103. // end
  104. $output .= $this->ln('</div>', $fieldLevel + 1);
  105. break;
  106. case 'SRV':
  107. // start
  108. $output .= $this->ln('<div class="d-flex align-items-center">', $fieldLevel + 1);
  109. // VALUE
  110. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  111. 'value="' . "{{ @\$contentData['{$key}'] }}" . '" ' .
  112. 'name="' . $key . '" type="text" class="form-control form-control-sm" placeholder="Answer">', $fieldLevel + 2);
  113. // UNKNOWN
  114. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 ml-3 mr-2">', $fieldLevel + 2);
  115. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  116. "{{ @\$contentData['{$key}_unknown'] ? 'checked' : '' }} " .
  117. 'name="' . $key . '_unknown" type="checkbox" class="mr-1">', $fieldLevel + 3);
  118. $output .= $this->ln('<span>Unknown</span>', $fieldLevel + 3);
  119. $output .= $this->ln('</label>', $fieldLevel + 2);
  120. // MEMO
  121. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  122. 'value="' . "{{ @\$contentData['{$key}_memo'] }}" . '" ' .
  123. 'name="' . $key . '_memo" type="text" class="form-control form-control-sm" placeholder="Memo">', $fieldLevel + 2);
  124. // end
  125. $output .= $this->ln('</div>', $fieldLevel + 1);
  126. break;
  127. case 'Text with Memo':
  128. // start
  129. $output .= $this->ln('<div class="d-flex align-items-center">', $fieldLevel + 1);
  130. // VALUE
  131. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  132. 'value="' . "{{ @\$contentData['{$key}'] }}" . '" ' .
  133. 'name="' . $key . '" type="text" class="form-control form-control-sm mr-2" placeholder="Answer">', $fieldLevel + 2);
  134. // MEMO
  135. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  136. 'value="' . "{{ @\$contentData['{$key}_memo'] }}" . '" ' .
  137. 'name="' . $key . '_memo" type="text" class="form-control form-control-sm" placeholder="Memo">', $fieldLevel + 2);
  138. // end
  139. $output .= $this->ln('</div>', $fieldLevel + 1);
  140. break;
  141. case 'Multi Checkbox with Other':
  142. // start
  143. $output .= $this->ln('<div class="mt-3">', $fieldLevel + 1);
  144. foreach ($_node->Options as $option) {
  145. $output .= $this->ln('<label class="d-flex align-items-center mb-1 mr-2">', $fieldLevel + 2);
  146. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  147. "{{ isset(\$contentData['{$key}']) && in_array('" . str_replace("'", "\\'", $option) . "', \$contentData['{$key}']) !== FALSE ? 'checked' : '' }} " .
  148. 'name="' . $key . '[]" value="' . $option . '" type="checkbox" class="mr-1">', $fieldLevel + 3);
  149. $output .= $this->ln('<span>' . $option . '</span>', $fieldLevel + 3);
  150. $output .= $this->ln('</label>', $fieldLevel + 2);
  151. }
  152. // OTHER
  153. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  154. 'value="' . "{{ @\$contentData['{$key}_other'] }}" . '" ' .
  155. 'name="' . $key . '_other" type="text" class="form-control form-control-sm my-3" placeholder="Other">', $fieldLevel + 2);
  156. // end
  157. $output .= $this->ln('</div>', $fieldLevel + 1);
  158. break;
  159. case 'Accept':
  160. // start
  161. $output .= $this->ln('<div class="d-flex align-items-center">', $fieldLevel + 1);
  162. // YES
  163. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $fieldLevel + 2);
  164. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  165. "{{ @\$contentData['{$key}'] === 'ACCEPT' ? 'checked' : '' }} " .
  166. 'name="' . $key . '" type="radio" value="ACCEPT" class="mr-1">', $fieldLevel + 3);
  167. $output .= $this->ln('<span>Accept</span>', $fieldLevel + 3);
  168. $output .= $this->ln('</label>', $fieldLevel + 2);
  169. // NO
  170. $output .= $this->ln('<label class="d-inline-flex align-items-center my-0 mr-3">', $fieldLevel + 2);
  171. $output .= $this->ln('<input onchange="onGemVChange_{{ $formID }}(this)" '.
  172. "{{ @\$contentData['{$key}'] === 'REJECT' ? 'checked' : '' }} " .
  173. 'name="' . $key . '" type="radio" value="REJECT" class="mr-1">', $fieldLevel + 3);
  174. $output .= $this->ln('<span>Reject</span>', $fieldLevel + 3);
  175. $output .= $this->ln('</label>', $fieldLevel + 2);
  176. // end
  177. $output .= $this->ln('</div>', $fieldLevel + 1);
  178. break;
  179. default:
  180. dump("Unknown type: {$_node->T}");
  181. break;
  182. }
  183. }
  184. // subs
  185. if (!!@$_node->S) {
  186. $output .= $this->ln('<div class="subs pl-4">', $_level + 2);
  187. foreach ($_node->S as $sub) {
  188. $output .= $this->gem($sub, $key, $_level + 2);
  189. }
  190. $output .= $this->ln('</div>', $_level + 2);
  191. }
  192. // close container
  193. $output .= $this->ln('</div>', $_level + 1);
  194. $output .= $this->nl();
  195. return $output;
  196. }
  197. public function formatText($_text) {
  198. $_text = preg_replace("/\{\{(.+?)\}\}/", '<span field="$1"></span>', $_text);
  199. return nl2br($_text);
  200. }
  201. public function ln($_line, $_indent = 1, $_nlAfter = true)
  202. {
  203. return str_repeat(" ", $_indent * 4) . $_line . ($_nlAfter ? "\n" : '');
  204. }
  205. public function nl()
  206. {
  207. return "\n";
  208. }
  209. }