v1.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace zin;
  3. requireWg('thinkQuestion');
  4. /**
  5. * 单选题型部件类
  6. * The thinkRadio widget class
  7. */
  8. class thinkRadio extends thinkQuestion
  9. {
  10. /**
  11. * @var mixed[]
  12. */
  13. protected static $defineProps = array
  14. (
  15. 'enableOther?: bool',
  16. 'fields?: array',
  17. 'setOption?: bool=false',
  18. 'quoteTitle?: string',
  19. 'quoteQuestions?: array',
  20. 'citation?: int=1',
  21. 'selectColumn?: string',
  22. 'isResult?: bool = false',
  23. );
  24. public static function getPageJS()
  25. {
  26. return file_get_contents(__DIR__ . DS . 'js' . DS . 'v1.js');
  27. }
  28. protected function buildDetail()
  29. {
  30. global $lang;
  31. $detailWg = parent::buildDetail();
  32. list($step, $mode, $isRun, $isResult, $quotedQuestions) = $this->prop(array('step', 'mode', 'isRun', 'isResult', 'quotedQuestions'));
  33. if($mode != 'detail') return array();
  34. $answer = $step->answer;
  35. $result = isset($answer->result) ? $answer->result : array();
  36. if(!empty($step->options->fields)) $step->options->fields = is_string($step->options->fields) ? explode(', ', $step->options->fields) : array_values((array)$step->options->fields);
  37. $fields = $step->options->fields ?? array();
  38. $items = array();
  39. if(!empty($fields)) foreach($fields as $field) $items[] = array('text' => $field, 'value' => $field);
  40. if(empty($fields) && !$isRun) $items[] = array('text' => $lang->thinkstep->optionReference, 'disabledPrefix' => true);
  41. if(!empty($step->options->enableOther)) $items[] = array('text' => $lang->other, 'value' => 'other', 'isOther' => '1', 'other' => isset($answer->other) ? $answer->other : '');
  42. $isQutoCheckbox = $step->options->questionType == 'checkbox' && !empty($step->options->setOption) && $step->options->setOption == 1;
  43. $viewDisabled = $isQutoCheckbox && !$isRun;
  44. $runDisabled = $isRun && !empty($quotedQuestions) && !empty($answer->result);
  45. $detailWg[] = thinkBaseCheckbox
  46. (
  47. set::type($step->options->questionType),
  48. set::items($items),
  49. set::name('result[]'),
  50. set::value($step->options->questionType == 'radio' ? ($result[0] ?? '') : $result),
  51. set::disabled($viewDisabled || $isResult || $runDisabled)
  52. );
  53. return $detailWg;
  54. }
  55. protected function buildFormItem()
  56. {
  57. global $lang, $app;
  58. $app->loadLang('thinkstep');
  59. $formItems = parent::buildFormItem();
  60. list($step, $questionType, $required, $enableOther, $fields, $setOption, $quoteQuestions, $quotedQuestions) = $this->prop(array('step', 'questionType', 'required', 'enableOther', 'fields', 'setOption', 'quoteQuestions', 'quotedQuestions'));
  61. $requiredItems = $lang->thinkstep->requiredList;
  62. if($step)
  63. {
  64. if(!empty($step->options->fields)) $step->options->fields = is_string($step->options->fields) ? explode(', ', $step->options->fields) : array_values((array)$step->options->fields);
  65. $enableOther = $step->options->enableOther ?? 0;
  66. $required = $step->options->required;
  67. $setOption = isset($step->options->setOption) ? $step->options->setOption : false;
  68. $fields = !empty($step->options->fields) ? $step->options->fields : array('', '', '');
  69. }
  70. $formItems[] = array(
  71. formHidden('options[questionType]', $questionType),
  72. $questionType == 'checkbox' ? thinkStepQuote(set::step($step), set::questionType($questionType), set::quoteQuestions($quoteQuestions), set::quotedQuestions($quotedQuestions)) : null,
  73. formGroup
  74. (
  75. setClass('think-options-field', ($questionType === 'checkbox' && $setOption == 1) ? 'hidden' : ''),
  76. set::label($lang->thinkstep->label->option),
  77. thinkOptions
  78. (
  79. set::name('options[fields]'),
  80. set::data($fields),
  81. set::otherName('options[enableOther]'),
  82. set::enableOther($enableOther)
  83. )
  84. ),
  85. formGroup
  86. (
  87. setClass('step-required'),
  88. setStyle(array('display' => 'flex')),
  89. setData('maxCountPlaceholder', $lang->thinkstep->placeholder->maxCount),
  90. setData('inputContent', $lang->thinkstep->placeholder->inputContent),
  91. set::label($lang->thinkstep->label->required),
  92. set::labelHint(!empty($quotedQuestions) ? $lang->thinkstep->tips->required : null),
  93. radioList
  94. (
  95. set::name('options[required]'),
  96. set::inline(true),
  97. set::value($required),
  98. set::items($requiredItems),
  99. set::disabled(!empty($quotedQuestions)),
  100. $questionType == 'checkbox' ? on::change()->toggleClass('.selectable-rows', 'hidden', 'target.value == 0') : null
  101. )
  102. ),
  103. $this->children()
  104. );
  105. return $formItems;
  106. }
  107. }