v1.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace zin;
  3. class thinkModel extends wg
  4. {
  5. /**
  6. * @var mixed[]
  7. */
  8. protected static $defineProps = array(
  9. 'mode?: string', // 模型展示模式。 preview 后台设计预览 | view 前台结果展示
  10. 'blocks: array', // 模型节点
  11. 'wizard?: object', // 模型数据
  12. 'previewKey?: int', // 区域组预览的键值
  13. );
  14. /**
  15. * @return \zin\wg|mixed[]
  16. * @param object $step
  17. */
  18. protected function buildQuestionItem($step)
  19. {
  20. $wizard = $this->prop('wizard');
  21. $questionType = $step->options->questionType;
  22. $wgMap = array('input' => 'thinkInput', 'radio' => 'thinkRadio', 'checkbox' => 'thinkCheckbox', 'tableInput' => 'thinkTableInput', 'multicolumn' => 'thinkMulticolumn');
  23. if(!isset($wgMap[$questionType])) return array();
  24. return createWg($wgMap[$questionType], array(set::step($step), set::questionType($questionType), set::mode('detail'), set::isResult(true), set::wizard($wizard)));
  25. }
  26. /**
  27. * @param object $step
  28. * @param int $blockID
  29. */
  30. protected function buildOptionsContent($step, $blockID)
  31. {
  32. global $app, $lang;
  33. $app->loadLang('thinkstep');
  34. if(isset($step->options->enableOther) && $step->options->enableOther == 'on') array_push($step->options->fields, 'other');
  35. $step->answer = !empty($step->answer) ? $step->answer : new stdClass();
  36. if(empty($step->answer->result)) $step->answer->result = array();
  37. $unselectedOptions = array_unique(array_diff($step->options->fields, $step->answer->result));
  38. $showOptions = !empty($step->link['selectedBlock']) && $step->link['selectedBlock'] == $blockID ? $step->answer->result : $unselectedOptions;
  39. $content = array();
  40. foreach($showOptions as $option)
  41. {
  42. if($option == 'other') $option = $step->answer->other ? $step->answer->other : $lang->other;
  43. if(!empty($option)) $content[] = div(setClass('mt-1 border p-1.5 break-all'), $option);
  44. }
  45. return empty($content) ? array() : array
  46. (
  47. div(setClass('text-lg mb-0.5'), $lang->thinkstep->label->option),
  48. $content
  49. );
  50. }
  51. /**
  52. * @param object $step
  53. */
  54. protected function buildMulticolumnContent($step)
  55. {
  56. global $app, $lang;
  57. $app->loadLang('thinkstep');
  58. $title = '';
  59. $colKey = $step->link['column'][0];
  60. if(isset($step->options->fields[$colKey - 1])) $title = $step->options->fields[$colKey - 1];
  61. $result = array();
  62. foreach($step->answer->result as $col => $answer)
  63. {
  64. $answerKey = 'col' . $colKey;
  65. if($col == $answerKey) $result = $answer;
  66. }
  67. $content = array();
  68. foreach($result as $item)
  69. {
  70. if(!empty($item)) $content[] = div(setClass('mt-1 border p-1.5 break-all'), $item);
  71. }
  72. return empty($content) ? array() : array
  73. (
  74. div(setClass('text-lg mb-0.5'), $lang->thinkstep->label->columnTitle . ': ' . $title),
  75. $content
  76. );
  77. }
  78. /**
  79. * @param object $data
  80. */
  81. protected function existEmptyCol($data)
  82. {
  83. foreach($data as $value)
  84. {
  85. if(!empty($value)) return false;
  86. }
  87. return true;
  88. }
  89. /**
  90. * @param object $data
  91. */
  92. protected function checkEmptyOfMulticolumn($data)
  93. {
  94. foreach($data as $item)
  95. {
  96. if(!$this->existEmptyCol($item)) return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. * @param mixed[] $steps
  102. * @param int $key
  103. * @param bool $isPosition
  104. */
  105. protected function buildResultCard($steps, $key, $isPosition = false)
  106. {
  107. $questionList = array();
  108. foreach($steps as $step)
  109. {
  110. if(is_string($step->link)) $step->link = json_decode($step->link, true);
  111. if(is_string($step->answer)) $step->answer = json_decode($step->answer);
  112. if(is_string($step->options)) $step->options = json_decode($step->options);
  113. $resultCard = array();
  114. $className = '';
  115. if($step->link['showMethod'] == 2)
  116. {
  117. $className = "card-{$step->options->questionType}";
  118. if($step->options->questionType == 'multicolumn' && $this->checkEmptyOfMulticolumn($step->answer->result))
  119. {
  120. $resultCard = array();
  121. }
  122. else
  123. {
  124. $resultCard = empty($step->answer->result) ? array() : $this->buildQuestionItem($step);
  125. }
  126. }
  127. elseif($step->link['showMethod'] == '1')
  128. {
  129. $className = 'card-options';
  130. $resultCard = $this->buildMulticolumnContent($step);
  131. }
  132. else
  133. {
  134. $className = 'card-options';
  135. $resultCard = $this->buildOptionsContent($step, $key);
  136. }
  137. $blockIndex = $key - 1;
  138. if($isPosition) $className .= ' overflow-y-auto hidden absolute ' . "in_area-{$blockIndex}";
  139. if(!empty($resultCard)) $questionList[] = div(setClass('w-64 bg-canvas scrollbar-thin p-2 shadow card ', $className), $resultCard);
  140. }
  141. return $questionList;
  142. }
  143. }