v1.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace zin;
  3. requireWg('thinkQuestion');
  4. /**
  5. * 思引师表格填空部件类。
  6. * thinmory tableInput widget class.
  7. */
  8. class thinkTableInput extends thinkQuestion
  9. {
  10. /**
  11. * @var mixed[]
  12. */
  13. protected static $defineProps = array(
  14. 'requiredRows?: number=1', // 必填行数
  15. 'fields?: array', // 行标题
  16. 'supportAdd?: bool', // 是否支持用户添加行
  17. 'canAddRows: number=1', // 可添加行数
  18. 'inputType?: bool=flase', // 输入类型
  19. );
  20. public static function getPageJS()
  21. {
  22. return file_get_contents(__DIR__ . DS . 'js' . DS . 'v1.js');
  23. }
  24. public static function getPageCSS()
  25. {
  26. $baseCss = file_get_contents(dirname(__FILE__, 2) . DS . 'thinkstepbase' . DS . 'css' . DS . 'v1.css');
  27. return file_get_contents(__DIR__ . DS . 'css' . DS . 'v1.css') . $baseCss;
  28. }
  29. protected function buildDetail()
  30. {
  31. global $lang, $config;
  32. $detailWg = parent::buildDetail();
  33. list($step, $fields, $supportAdd, $canAddRows, $mode, $inputType, $wizard, $quotedQuestions) = $this->prop(array('step', 'fields', 'supportAdd', 'canAddRows', 'mode', 'inputType', 'wizard', 'quotedQuestions'));
  34. if($mode != 'detail') return array();
  35. $wizard->config = !empty($wizard->config) ? $wizard->config : array();
  36. $wizard->config = is_string($wizard->config) ? json_decode($wizard->config, true) : $wizard->config;
  37. $configureDimension = !empty($wizard->config['configureDimension']) ? $wizard->config['configureDimension'] : array();
  38. $canConfigureRatio = !empty($configureDimension['canConfigureRatio']) ? $configureDimension['canConfigureRatio'] : '0';
  39. if($step)
  40. {
  41. $fields = $step->options->fields;
  42. $supportAdd = $step->options->supportAdd;
  43. $canAddRows = $step->options->canAddRows;
  44. $answer = $step->answer;
  45. $result = isset($answer->result) && !empty($answer->result) ? (array) $answer->result : array();
  46. $customFields = !empty($answer->customFields) ? get_object_vars($answer->customFields) : array();
  47. $inputType = isset($step->options->inputType) ? $step->options->inputType : false;
  48. }
  49. $tableInputItems = array();
  50. $disabledAdd = !empty($customFields) && (int)$canAddRows <= count($customFields);
  51. $fields = !empty($fields) ? $fields : array($lang->thinkwizard->rowReference, $lang->thinkwizard->rowReference, $lang->thinkwizard->rowReference);
  52. $index = 0;
  53. foreach($fields as $item)
  54. {
  55. $value = !empty($result) && isset($result[$index]) ? $result[$index] : '';
  56. $amountUnit = !empty($answer->amountUnit) ? $answer->amountUnit[$index] : '';
  57. $tableInputItems[] = array(
  58. formGroup
  59. (
  60. setClass('flex items-center table-input-item'),
  61. div
  62. (
  63. setClass('text-right mr-2 w-24 line-clamp-2 break-words'),
  64. $item,
  65. set::title($item)
  66. ),
  67. div(set::title($value), setClass(empty($inputType) ? 'result-width' : '', $index == 0 ? '' : 'mt-2'), empty($inputType) ? textarea
  68. (
  69. set::rows('2'),
  70. set::name('result[' . $index . ']'),
  71. set::value($value),
  72. set::placeholder($lang->thinkrun->pleaseInput),
  73. set::readonly($value && !empty($quotedQuestions))
  74. ) : null, (!empty($inputType) && $inputType == '1') ? inputControl
  75. (
  76. input(set(array(
  77. 'class' => 'w-72 h-10 dimension-weight',
  78. 'name' => 'result[' . $index . ']',
  79. 'type' => 'number',
  80. 'min' => 1,
  81. 'max' => 100,
  82. 'value' => $value,
  83. 'readonly' => ($value && !$canConfigureRatio),
  84. 'placeholder' => $lang->thinkrun->pleaseInput
  85. )),
  86. on::input('changePercentInput')
  87. ),
  88. to::suffix($lang->thinkwizard->dimension->percentageSign),
  89. set::suffixWidth(32)
  90. ) : null, (!empty($inputType) && $inputType == '2') ? inputGroup
  91. (
  92. input(set(array(
  93. 'class' => 'w-64 h-10 result-amount',
  94. 'name' => 'result[' . $index . ']',
  95. 'type' => 'number',
  96. 'min' => 0,
  97. 'value' => $value,
  98. 'readonly' => ($value && !empty($quotedQuestions)),
  99. 'placeholder' => $lang->thinkrun->pleaseInput
  100. )),
  101. on::input('changePriceInput')
  102. ),
  103. picker(setClass('amount-unit'), set::name('amountUnit[' . $index . ']'), set::required(true), set::items($lang->thinkrun->amountUnit), set::value($amountUnit))
  104. )
  105. : null),
  106. div(setClass('flex'), setStyle(array('min-width' => '40px')), ($index == count($fields) - 1 && $supportAdd) ? icon('plus', setClass('mr-1 btn-add ml-2 text-sm text-primary add-rows cursor-pointer', $disabledAdd ? 'disabled' : ''), setData('canAddRows', $canAddRows), setData('fieldsCount', count($fields)), set::title(sprintf($lang->thinkrun->tips->add, $canAddRows))) : null)
  107. ),
  108. );
  109. $index ++;
  110. }
  111. if(!empty($customFields))
  112. {
  113. foreach($customFields as $item)
  114. {
  115. $customValue = !empty($result) && isset($result[$index]) ? $result[$index] : '';
  116. $tableInputItems[] = formGroup
  117. (
  118. setClass('flex rows-group flex-nowrap items-center'),
  119. div
  120. (
  121. set::title($item),
  122. setClass('mt-2 w-24'),
  123. textarea
  124. (
  125. set::rows('2'),
  126. setID('customFields'),
  127. set::name('customFields[' . $index . ']'),
  128. set::value($item),
  129. set::placeholder($lang->thinkrun->placeholder->rowTitle)
  130. )
  131. ),
  132. div
  133. (
  134. set::title($customValue),
  135. setClass('mt-2 ml-2 result-width'),
  136. textarea
  137. (
  138. set::rows('2'),
  139. setID('result'),
  140. set::name('result[' . $index .']'),
  141. set::value($customValue),
  142. set::placeholder($lang->thinkrun->placeholder->rowContent)
  143. )
  144. ),
  145. div
  146. (
  147. setClass('flex'),
  148. setStyle(array('min-width' => '40px')),
  149. icon('plus', setClass('mr-1 btn-add ml-2 text-sm text-primary add-rows', $disabledAdd ? 'disabled' : ''), setData('canAddRows', $canAddRows), setData('fieldsCount', count($fields)), set::title(sprintf($lang->thinkrun->tips->add, $canAddRows))),
  150. icon
  151. (
  152. setClass('btn-delete text-sm text-primary ml-1'),
  153. setData('canAddRows', $canAddRows),
  154. setData('fieldsCount', count($fields)),
  155. setData('deleteTip', $lang->thinkrun->tips->delete),
  156. 'trash'
  157. )
  158. )
  159. );
  160. $index ++;
  161. }
  162. }
  163. $detailWg[] = array(
  164. $tableInputItems,
  165. formGroup
  166. (
  167. setClass('flex rows-template flex-nowrap items-center hidden'),
  168. textarea
  169. (
  170. set::rows('2'),
  171. setID('customFields'),
  172. setClass('mt-2 w-24'),
  173. set::value(''),
  174. set::placeholder($lang->thinkrun->placeholder->rowTitle)
  175. ),
  176. textarea
  177. (
  178. set::rows('2'),
  179. setID('result'),
  180. setClass('mt-2 result-width ml-2'),
  181. set::value(''),
  182. set::placeholder($lang->thinkrun->placeholder->rowContent)
  183. ),
  184. div
  185. (
  186. setClass('flex'),
  187. setStyle(array('min-width' => '40px')),
  188. icon('plus', setClass('mr-1 btn-add ml-2 text-sm text-primary add-rows cursor-pointer'), setData('canAddRows', $canAddRows), setData('fieldsCount', count($fields)), set::title(sprintf($lang->thinkrun->tips->add, $canAddRows))),
  189. icon
  190. (
  191. setClass('btn-delete text-sm text-primary ml-1 cursor-pointer'),
  192. setData('canAddRows', $canAddRows),
  193. setData('fieldsCount', count($fields)),
  194. setData('deleteTip', $lang->thinkrun->tips->delete),
  195. 'trash'
  196. )
  197. )
  198. )
  199. );
  200. return $detailWg;
  201. }
  202. protected function buildFormItem()
  203. {
  204. global $lang, $app;
  205. $app->loadLang('thinkstep');
  206. $formItems = parent::buildFormItem();
  207. list($step, $required, $requiredRows, $supportAdd, $canAddRows, $fields, $inputType) = $this->prop(array('step','required', 'requiredRows', 'supportAdd', 'canAddRows', 'fields', 'inputType'));
  208. if($step)
  209. {
  210. $required = $step->options->required;
  211. if(!empty($step->options->fields)) $step->options->fields = is_string($step->options->fields) ? explode(', ', $step->options->fields) : array_values((array)$step->options->fields);
  212. $fields = $step->options->fields ?? array();
  213. $requiredRows = $step->options->requiredRows;
  214. $supportAdd = $step->options->supportAdd;
  215. $canAddRows = $step->options->canAddRows;
  216. $inputType = isset($step->options->inputType) ? $step->options->inputType : false;
  217. }
  218. $formItems[] = array (
  219. formHidden('options[questionType]', 'tableInput'),
  220. formHidden('options[inputType]', $inputType),
  221. formRow
  222. (
  223. setClass('mb-3'),
  224. formGroup
  225. (
  226. setClass('w-1/2 step-required'),
  227. set::label($lang->thinkstep->label->required),
  228. radioList
  229. (
  230. set::name('options[required]'),
  231. set::inline(true),
  232. set::items($lang->thinkstep->requiredList),
  233. set::value($required),
  234. on::change()->toggleClass('.required-rows', 'hidden', 'target.value == 0')
  235. )
  236. ),
  237. formGroup
  238. (
  239. setClass('w-1/2 required-rows', $required ? '' : 'hidden'),
  240. set::label($lang->thinkstep->label->requiredRows),
  241. set::labelClass('required'),
  242. input
  243. (
  244. set::type('number'),
  245. set::name('options[requiredRows]'),
  246. set::value($requiredRows),
  247. set::placeholder($lang->thinkstep->placeholder->inputContent),
  248. set::min(1),
  249. on::input('changeInput')
  250. )
  251. )
  252. ),
  253. formRow
  254. (
  255. setClass('mb-3'),
  256. formGroup
  257. (
  258. setClass('w-1/2'),
  259. set::label($lang->thinkstep->label->isSupportAdd),
  260. radioList
  261. (
  262. set::name('options[supportAdd]'),
  263. set::inline(true),
  264. set::items($lang->thinkstep->requiredList),
  265. set::value($supportAdd ? $supportAdd : 0),
  266. on::change()->toggleClass('.can-add-rows', 'hidden', 'target.value == 0')
  267. )
  268. ),
  269. formGroup
  270. (
  271. setClass('w-1/2 can-add-rows', $supportAdd ? '' : 'hidden'),
  272. set::label($lang->thinkstep->label->canAddRows),
  273. set::labelClass('required'),
  274. input
  275. (
  276. set::type('number'),
  277. set::name('options[canAddRows]'),
  278. set::value($canAddRows),
  279. set::placeholder($lang->thinkstep->placeholder->inputContent),
  280. set::min(1),
  281. on::input('changeInput')
  282. )
  283. )
  284. ),
  285. formGroup(set::width('full'), set::label($lang->thinkstep->label->rowsTitle), thinkoptions
  286. (
  287. set(array(
  288. 'showOther' => false,
  289. 'data' => $fields,
  290. 'name' => 'options[fields]',
  291. ))
  292. )),
  293. $this->children()
  294. );
  295. return $formItems;
  296. }
  297. }